-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35_permutationinstring.cpp
More file actions
39 lines (38 loc) · 992 Bytes
/
35_permutationinstring.cpp
File metadata and controls
39 lines (38 loc) · 992 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
37
38
39
//https://leetcode.com/problems/permutation-in-string/description/
class Solution {
public:
bool checkInclusion(string s1, string s2) {
unordered_map< char, int >mp;
for(auto it : s1){
mp[it]++;
}
int count = mp.size();
int i = 0, j = 0;
int k = s1.size();
while(j < s2.size()){
if(mp.find(s2[j]) != mp.end()){
mp[s2[j]]--;
if(mp[s2[j]] == 0){
count--;
}
}
if(j-i+1 < k){
j++;
}
else if(j-i+1 == k){
if(count == 0){
return true;
}
if(mp.find(s2[i]) != mp.end()){
mp[s2[i]]++;
if(mp[s2[i]] == 1){
count++;
}
}
i++;
j++;
}
}
return false;
}
};