File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed
0438_find_all_anagrams_in_a_string
0567_permutation_in_string Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ class Solution {
6
+ public:
7
+ vector<int > findAnagrams (string s, string p) {
8
+ int count[128 ] = { 0 };
9
+ for (char c : p) {
10
+ count[c]++;
11
+ }
12
+
13
+ vector<int > res;
14
+ int l = 0 , r = 0 , len = 0 ;
15
+ while (r < s.length ()) {
16
+ if (--count[s[r++]] >= 0 ) {
17
+ len++;
18
+ }
19
+
20
+ if (r - l >= p.length ()) {
21
+ if (len == p.length ()) {
22
+ res.push_back (l);
23
+ }
24
+ if (++count[s[l++]] > 0 ) {
25
+ len--;
26
+ }
27
+ }
28
+ }
29
+
30
+ return res;
31
+ }
32
+ };
Original file line number Diff line number Diff line change
1
+ #include < stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ class Solution {
6
+ public:
7
+ bool checkInclusion (string s1, string s2) {
8
+ int count[128 ] = { 0 };
9
+ for (char c : s1) {
10
+ count[c]++;
11
+ }
12
+
13
+ int l = 0 , r = 0 , len = 0 ;
14
+ while (r < s2.length ()) {
15
+ if (--count[s2[r++]] >= 0 ) {
16
+ len++;
17
+ }
18
+
19
+ if (r - l >= s1.length ()) {
20
+ if (len == s1.length ()) {
21
+ return true ;
22
+ }
23
+ if (++count[s2[l++]] > 0 ) {
24
+ len--;
25
+ }
26
+ }
27
+ }
28
+
29
+ return false ;
30
+ }
31
+ };
You can’t perform that action at this time.
0 commit comments