-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17.cpp
60 lines (36 loc) · 788 Bytes
/
Day17.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
49
50
51
52
53
54
55
56
57
58
59
60
class Solution
{
public:
vector<int> findAnagrams(string s, string p)
{
vector < int > ans;
ans.clear() ;
if(s.empty())
return ans;
if( p.length() > s.length() )
{
return ans;
}
map < char, int > mp,mp2;
for(auto cur:p)
mp[cur]++;
for(int i = 0 ; i < p.length() ; i++)mp2[ s[i] ]++;
int k = p.length() ;
for(int i = 0 ; i <= s.length() - k ; i++)
{
if( mp2 == mp )
{
ans.push_back(i);
}
if( mp2[ s[i] ]>0 )
{
mp2[ s[i] ]--;
if( mp2[ s[i] ] <= 0 ){
mp2.erase( s[i] );
}
}
mp2[ s[ i+k ] ]++;
}
return ans;
}
};