-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_All_Anagrams_In_String.cpp
More file actions
33 lines (33 loc) · 988 Bytes
/
Copy pathFind_All_Anagrams_In_String.cpp
File metadata and controls
33 lines (33 loc) · 988 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
class Solution {
bool compares(unordered_map<char,int>& pat, unordered_map<char,int>& txt){
for(auto it = txt.begin(); it!=txt.end(); it++)
if(!(pat.count(it->first) && it->second==pat[it->first]))
return false;
return true;
}
public:
vector<int> findAnagrams(string s, string p) {
int window = p.size();
int n = s.size();
if(n<window)
return {};
unordered_map<char,int> pat,tmp;
for(auto ch: p)
pat[ch]++;
vector<int> res;
for(int i=0; i<window; i++)
tmp[s[i]]++;
if(compares(pat,tmp))
res.push_back(0);
for(int j = window; j<n; j++){
int start = j-window+1;
tmp[s[start-1]]--;
tmp[s[j]]++;
if(tmp[s[start-1]]==0)
tmp.erase(s[start-1]);
if(compares(pat,tmp))
res.push_back(start);
}
return res;
}
};