-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation_in_string.cpp
More file actions
51 lines (41 loc) · 1.07 KB
/
Copy pathpermutation_in_string.cpp
File metadata and controls
51 lines (41 loc) · 1.07 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
class Solution {
private:
bool checker(int a[26], int b[26]){
for(int i=0; i<26; i++){
if(a[i]!=b[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 windowSize = s1.length();
int count2[26]={0};
while(i < windowSize && i < s2.length()){
int index = s2[i] - 'a';
count2[index]++;
i++;
}
if(checker(count1, count2))
return 1;
while(i<s2.length()){
char newChar = s2[i];
int index = newChar-'a';
count2[index]++;
char oldChar = s2[i-windowSize];
index = oldChar - 'a';
count2[index]--;
i++;
if(checker(count1, count2))
return 1;
}
return 0;
}
};