-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFindAllAnagramsInAString.cpp
48 lines (39 loc) · 1.04 KB
/
FindAllAnagramsInAString.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
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
map<char, int> forP;
for(int i = 0; i < p.size(); i++){
if(forP.find(p[i]) == forP.end()){
forP[p[i]] = 1;
}
else{
forP[p[i]]++;
}
}
int start = 0, i = 0, n = s.size();
map<char, int> m;
vector<int> sol;
while(i < n){
if(m.find(s[i]) == m.end()){
m[s[i]] = 1;
}
else{
m[s[i]]++;
}
if((i - start) == p.size()){
if(m[s[start]] == 1){
m.erase(m.find(s[start]));
}
else{
m[s[start]]--;
}
start++;
}
if(m == forP){
sol.push_back(start);
}
i++;
}
return sol;
}
};