-
Notifications
You must be signed in to change notification settings - Fork 0
/
22_permutaionString.cpp
69 lines (62 loc) · 1.53 KB
/
22_permutaionString.cpp
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
#include <iostream>
#include <iomanip>
using namespace std;
class Solution {
private:
bool checkEqual(int arr1[26],int arr2[26]){
for(int i=0; i<26; i++){
if(arr1[i]!=arr2[i]){
return 0;
}
}
return 1;
}
public:
bool checkInclusion(string s1, string s2) {
// Base case:
if(s1.length()>s2.length()){
return 0;
}
// character count array for small string:
int arr1[26]={0};
for(int i=0; i<s1.length(); i++){
int index= s1[i]-'a';
arr1[index]++;
}
// Windowsize bana ke traverse in large string:
int i=0;
int windowSize=s1.length();
int arr2[26]={0};
// runnig for first window:
while(i<windowSize ){
int index=s2[i]-'a';
arr2[index]++;
i++;
}
if (checkEqual(arr1,arr2)){
return 1;
}
// Aage ki window ko process krte hai:
while(i<s2.length()){
char newChar= s2[i];
int index= newChar-'a';
arr2[index]++;
// old character ko bahar nikal rahe hai:
char oldChar= s2[i-windowSize];
index= oldChar-'a';
arr2[index]--;
i++;
if (checkEqual(arr1,arr2)){
return 1;
}
}
return 0;
}
};
int main() {
Solution sol;
string s1="ab";
string s2="eidbaooo";
cout<<sol.checkInclusion(s1,s2);
return 0;
}