forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path438.cpp
More file actions
47 lines (45 loc) · 1.14 KB
/
438.cpp
File metadata and controls
47 lines (45 loc) · 1.14 KB
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
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
if (s.length() < p.length()) {
vector<int> v;
return v;
}
unordered_map<char,int> t , m ;
for(char c : p) {
t[c]++;
}
for(int i=0;i<p.length();i++){
m[s[i]]++;
}
vector<int> ans;
int st=0, en=p.length()-1;
while(en<s.length()) {
if (same(m,t)) {
ans.push_back(st);
}
acq(m, st, en, s);
rel(m, st, en, s);
}
//
return ans;
}
void acq(unordered_map<char,int> & m, int &st, int &en, string &str) {
en++;
if (en<str.length()) {
m[str[en]]++;
}
}
void rel(unordered_map<char,int> & m, int &st, int &en, string &str) {
m[str[st]]--;
if (m[str[st]]==0) m.erase(str[st]);
st++;
}
bool same(unordered_map<char,int> & m, unordered_map<char,int> & t) {
for(auto p : t) {
if (m[p.first] != p.second)
return false;
}
return true;
}
};