-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path438.cpp
More file actions
31 lines (31 loc) · 693 Bytes
/
438.cpp
File metadata and controls
31 lines (31 loc) · 693 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
// myidea.cpp
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
vector<int> res;
vector<int> letter(26, 0);
int total = p.size(), start = 0;
for (int i = 0; i < p.size(); ++i)
letter[p[i] - 'a']++;
for (int i = 0; i < s.size();)
if (letter[s[i] - 'a'] > 0) {
letter[s[i] - 'a']--;
++i;
if (--total == 0) {
total = 1;
letter[s[start] - 'a']++;
res.push_back(start++);
}
} else {
if (i > start) {
letter[s[start] - 'a']++;
start++;
total++;
} else {
++i;
++start;
}
}
return res;
}
};