-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path438.cpp
More file actions
executable file
·55 lines (46 loc) · 1.42 KB
/
438.cpp
File metadata and controls
executable file
·55 lines (46 loc) · 1.42 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
48
49
50
51
52
53
54
55
# include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
unordered_map<char,int> numbers;
for (auto c : p){
if (!numbers.count(c)){
numbers[c] = 1;
}else{
numbers[c] += 1;
}
}
unordered_map<char,int>
int start = 0, end = 0;
vector<int> res;
while (start < s.size() - p.size() + 1){
if (start == end && !numbers.count(s[start])){
while (start < (s.size() - p.size() + 1) && !numbers.count(s[start]))
start ++;
end = start;
}
if (start < s.size() - p.size() + 1){
// expand end
while (end - start + 1 < p.size()){
end ++;
if (!numbers.count(s[end])){
start = ++end;
break;
}
if (numbers[s[end]] <= 0){
start ++;
break;
}
numbers[s[end]] --;
}
if (end - start + 1 == p.size()){
res.push_back(start);
numbers[s[start++]] += 1;
}
// shrink start
}
}
return res;
}
};