-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18 - Permutation in String.cpp
37 lines (37 loc) · 1 KB
/
18 - 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
35
36
37
class Solution {
public:
bool checkInclusion(string s1, string s2) {
unordered_map <char, int> mp;
int counter = s1.size();
int left = 0;
for(int i = 0; i<s1.size(); i++){
mp[s1[i]]++;
}
for(int i = 0; i<s2.size(); i++){
if(mp.find(s2[i]) != mp.end() && mp[s2[i]]){
counter--;
mp[s2[i]]--;
if(counter == 0){
return 1;
}
}
else{
while(left < i){
if(mp.find(s2[left]) != mp.end()){
mp[s2[left]]++;
counter++;
}
left++;
if(mp.find(s2[i]) != mp.end() && mp[s2[i]]){
i--;
break;
}
}
}
if(mp.find(s2[i]) == mp.end()){
left++;
}
}
return 0;
}
};