-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation_in_a_string.cpp
More file actions
36 lines (28 loc) 路 993 Bytes
/
Copy pathpermutation_in_a_string.cpp
File metadata and controls
36 lines (28 loc) 路 993 Bytes
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
// https://leetcode.com/problems/permutation-in-string/
class Solution {
public:
bool checkInclusion(string s1, string s2) {
size_t len1 = s1.length();
size_t len2 = s2.length();
if (len1 > len2) return false;
int window_size = len1;
int total_stride = len2 - len1 + 1;
vector<int> h_map(26,0);
for(int i = 0; i < len1; i++) h_map[s1[i]-'a']++;
int k = 0;
unsigned int permNum;
vector<int> tmp_arr(26,0);
while(k < total_stride){
fill(tmp_arr.begin(), tmp_arr.end(), 0);
permNum = 0;
for(int i = 0; i < len1; i++) tmp_arr[s2[k+i]-'a']++;
k++;
for (int i = 0; i < 26; i++) {
if(h_map[i] != tmp_arr[i]) break;
permNum++;
}
if (permNum == tmp_arr.size()) return true;
}
return false;
}
};