-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
71 lines (61 loc) · 1.56 KB
/
Solution.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
61
62
63
64
65
66
67
68
69
70
71
static const auto io_speed_up = []()
{
std::ios::sync_with_stdio(false) ;
cin.tie(nullptr) ;
return 0 ;
}() ;
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
int l = s.size() - p.size() ;
if (l < 0)
return {} ;
vector<int> pt(128, 0) ;
vector<int> st(128, 0) ;
vector<int> res ;
for (auto ch: p)
++pt[ch] ;
for (int i = 0; i <= l; ++i)
{
for (int j = 0; j <= p.size(); ++j)
{
if (p.size() == j)
{
if (match(st, pt))
{
res.push_back(i) ;
}
--st[s[i]] ;
++i ;
j-=2 ;
continue ;
}
char ch = s[i+j] ;
if (0 == pt[ch])
{
i = i+j ;
clear(st) ;
break ;
}
if (pt[ch] < ++st[ch])
{
clear(st) ;
break ;
}
}
}
return res ;
}
inline void clear(vector<int> &v)
{
for (int i = 'a'; i <= 'z'; ++i)
v[i] = 0 ;
}
inline bool match(vector<int> &a, vector<int> &b)
{
for (int i = 'a'; i <= 'z'; ++i)
if (a[i] != b[i])
return false ;
return true ;
}
};