-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindpermutation.cpp
More file actions
74 lines (51 loc) · 1.31 KB
/
findpermutation.cpp
File metadata and controls
74 lines (51 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
class Solution {
private:
bool checkEqual(int count1[26],int count2[26]){
for(int i = 0; i<26; i++){
if(count1[i]!=count2[i]){
return 0;
}
}
return 1;
}
public:
bool checkInclusion(string s1, string s2) {
int count1[26] = {0};
for(int i=0; i<s1.length(); i++){
int index = s1[i] - 'a';
count1[index]++;
}
int i = 0;
int count2[26]={0};
while(i < s1.length() && i < s2.length()){
int index = s2[i] - 'a';
count2[index]++;
i++;
}
if(checkEqual(count1,count2)){
return 1;
}
while(i<s2.length()){
int newChar = s2[i] ;
int index = newChar - 'a';
count2[index]++;
int oldChar = s2[i-s1.length()];
index = oldChar - 'a';
count2[index]--;
if(checkEqual(count1,count2)){
return 1;
}
i++;
}
return 0;
}
};
int main(){
string s1, s2;
cout<<"Enter the strins : ";
cin>>s1>>s2;
Solution s;
cout<<"The answer is : "<<s.checkInclusion(s1,s2);
}