File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed
solution/438.Find All Anagrams in a String Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change
1
+ static const auto io_speed_up = []()
2
+ {
3
+ std::ios::sync_with_stdio (false ) ;
4
+ cin.tie (nullptr ) ;
5
+ return 0 ;
6
+ }() ;
7
+
8
+ class Solution {
9
+ public:
10
+ vector<int > findAnagrams (string s, string p) {
11
+ int l = s.size () - p.size () ;
12
+ if (l < 0 )
13
+ return {} ;
14
+ vector<int > pt (128 , 0 ) ;
15
+ vector<int > st (128 , 0 ) ;
16
+ vector<int > res ;
17
+
18
+ for (auto ch: p)
19
+ ++pt[ch] ;
20
+
21
+ for (int i = 0 ; i <= l; ++i)
22
+ {
23
+ for (int j = 0 ; j <= p.size (); ++j)
24
+ {
25
+ if (p.size () == j)
26
+ {
27
+ if (match (st, pt))
28
+ {
29
+ res.push_back (i) ;
30
+ }
31
+ --st[s[i]] ;
32
+ ++i ;
33
+ j-=2 ;
34
+ continue ;
35
+ }
36
+
37
+ char ch = s[i+j] ;
38
+ if (0 == pt[ch])
39
+ {
40
+ i = i+j ;
41
+ clear (st) ;
42
+ break ;
43
+ }
44
+
45
+ if (pt[ch] < ++st[ch])
46
+ {
47
+ clear (st) ;
48
+ break ;
49
+ }
50
+ }
51
+ }
52
+
53
+ return res ;
54
+ }
55
+
56
+ inline void clear (vector<int > &v)
57
+ {
58
+ for (int i = ' a' ; i <= ' z' ; ++i)
59
+ v[i] = 0 ;
60
+ }
61
+
62
+ inline bool match (vector<int > &a, vector<int > &b)
63
+ {
64
+ for (int i = ' a' ; i <= ' z' ; ++i)
65
+ if (a[i] != b[i])
66
+ return false ;
67
+ return true ;
68
+ }
69
+
70
+
71
+ };
You can’t perform that action at this time.
0 commit comments