-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation-in-string.cpp
34 lines (34 loc) · 1.07 KB
/
permutation-in-string.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
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int s1Size = s1.size(), s2Size = s2.size();
if (s1Size > s2.size()) {
return false;
}
unordered_map<char, int> counts;
for (char c : s1) {
counts[c]++;
}
for (int i = 0; i < s2Size; i++) {
// Possible substring match
if (counts.find(s2[i]) != counts.end()) {
// Check valid substring
bool found = true;
unordered_map<char, int> substrMap;
for (int k = s1Size, s2Idx = i; k > 0; k--, s2Idx++) {
if (counts.find(s2[s2Idx]) == counts.end()) {
i = s2Idx;
found = false;
break;
}
substrMap[s2[s2Idx]]++;
}
// Check if valid substring found
if (found && counts == substrMap) {
return true;
}
}
}
return false;
}
};