-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation_strint.cpp
More file actions
66 lines (53 loc) · 1.34 KB
/
permutation_strint.cpp
File metadata and controls
66 lines (53 loc) · 1.34 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool checkequal(int a[26], int b[26]) {
for (int i = 0; i < 26; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
bool checkinclusion(string s1, string s2) {
int count1[26] = {0};
// Count frequency of s1
for (int i = 0; i < s1.length(); i++) {
int index = s1[i] - 'a';
count1[index]++;
}
int windowsize = s1.length();
int count2[26] = {0};
// First window
int i = 0;
while (i < windowsize && i < s2.size()) {
int index = s2[i] - 'a';
count2[index]++;
i++;
}
if (checkequal(count1, count2)) {
return true;
}
// Sliding window
while (i < s2.length()) {
char newchar = s2[i];
count2[newchar - 'a']++;
char oldchar = s2[i - windowsize];
count2[oldchar - 'a']--;
if (checkequal(count1, count2)) {
return true;
}
i++; // move window forward
}
return false; // return false if no permutation found
}
int main() {
string s1, s2;
cout << "Enter s1: ";
cin >> s1;
cout << "Enter s2: ";
cin >> s2;
cout << (checkinclusion(s1, s2) ? "true" : "false") << endl;
return 0;
}