File tree Expand file tree Collapse file tree 12 files changed +996
-359
lines changed
剑指 Offer II 014. 字符串中的变位词
剑指 Offer II 015. 字符串中的所有变位词 Expand file tree Collapse file tree 12 files changed +996
-359
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1
- class Solution {
2
- public:
3
- bool checkInclusion (string s1, string s2) {
4
-
5
- int len1 = s1.size ();
6
- int len2 = s2.size ();
7
-
8
- if (len2 < len1) {
9
- return false ;
10
- }
11
-
12
- int count[30 ] = {0 };
13
-
14
- for (int i = 0 ; i < len1; ++i) {
15
- ++count[s1[i] - ' a' ];
16
- --count[s2[i] - ' a' ];
17
- }
18
-
19
- int l = 0 ;
20
- int r = len1 - 1 ;
21
-
22
- while (r < len2) {
23
-
24
- bool flag = true ;
25
-
26
- for (int i : count) {
27
- if (i != 0 ) {
28
- flag = false ;
29
- }
30
- }
31
-
32
- if (flag) {
33
- return true ;
34
- }
35
-
36
- if (r + 1 >= len2) {
37
- break ;
38
- }
39
-
40
- ++count[s2[l++] - ' a' ];
41
- --count[s2[++r] - ' a' ];
42
- }
43
-
44
- return false ;
45
- }
1
+ class Solution {
2
+ public:
3
+ bool checkInclusion (string s1, string s2) {
4
+ int m = s1.size (), n = s2.size ();
5
+ if (m > n) {
6
+ return false ;
7
+ }
8
+ vector<int > cnt (26 );
9
+ for (int i = 0 ; i < m; ++i) {
10
+ --cnt[s1[i] - ' a' ];
11
+ ++cnt[s2[i] - ' a' ];
12
+ }
13
+ int diff = 0 ;
14
+ for (int x : cnt) {
15
+ if (x != 0 ) {
16
+ ++diff;
17
+ }
18
+ }
19
+ if (diff == 0 ) {
20
+ return true ;
21
+ }
22
+ for (int i = m; i < n; ++i) {
23
+ int a = s2[i - m] - ' a' ;
24
+ int b = s2[i] - ' a' ;
25
+ if (cnt[a] == 0 ) {
26
+ ++diff;
27
+ }
28
+ --cnt[a];
29
+ if (cnt[a] == 0 ) {
30
+ --diff;
31
+ }
32
+ if (cnt[b] == 0 ) {
33
+ ++diff;
34
+ }
35
+ ++cnt[b];
36
+ if (cnt[b] == 0 ) {
37
+ --diff;
38
+ }
39
+ if (diff == 0 ) {
40
+ return true ;
41
+ }
42
+ }
43
+ return false ;
44
+ }
46
45
};
Original file line number Diff line number Diff line change 1
1
func checkInclusion (s1 string , s2 string ) bool {
2
- n1 , n2 := len (s1 ), len (s2 )
3
- if n1 > n2 {
2
+ m , n := len (s1 ), len (s2 )
3
+ if m > n {
4
4
return false
5
5
}
6
- window := make ([ ]int , 26 )
7
- for i := 0 ; i < n1 ; i ++ {
8
- window [s1 [i ]- 'a' ]++
9
- window [s2 [i ]- 'a' ]--
6
+ cnt := [ 26 ]int {}
7
+ for i := 0 ; i < m ; i ++ {
8
+ cnt [s1 [i ]- 'a' ]--
9
+ cnt [s2 [i ]- 'a' ]++
10
10
}
11
- if check (window ) {
11
+ diff := 0
12
+ for _ , x := range cnt {
13
+ if x != 0 {
14
+ diff ++
15
+ }
16
+ }
17
+ if diff == 0 {
12
18
return true
13
19
}
14
- for i := n1 ; i < n2 ; i ++ {
15
- window [s2 [i ]- 'a' ]--
16
- window [s2 [i - n1 ]- 'a' ]++
17
- if check (window ) {
20
+ for i := m ; i < n ; i ++ {
21
+ a , b := s2 [i - m ]- 'a' , s2 [i ]- 'a'
22
+ if cnt [a ] == 0 {
23
+ diff ++
24
+ }
25
+ cnt [a ]--
26
+ if cnt [a ] == 0 {
27
+ diff --
28
+ }
29
+ if cnt [b ] == 0 {
30
+ diff ++
31
+ }
32
+ cnt [b ]++
33
+ if cnt [b ] == 0 {
34
+ diff --
35
+ }
36
+ if diff == 0 {
18
37
return true
19
38
}
20
39
}
21
40
return false
22
- }
23
-
24
- func check (window []int ) bool {
25
- for _ , cnt := range window {
26
- if cnt != 0 {
27
- return false
28
- }
29
- }
30
- return true
31
41
}
Original file line number Diff line number Diff line change 1
- class Solution {
2
- public boolean checkInclusion (String s1 , String s2 ) {
3
- int n1 = s1 .length (), n2 = s2 .length ();
4
- if (n1 > n2 ) {
5
- return false ;
6
- }
7
- int [] window = new int [26 ];
8
- for (int i = 0 ; i < n1 ; i ++) {
9
- window [s1 .charAt (i ) - 'a' ]++;
10
- window [s2 .charAt (i ) - 'a' ]--;
11
- }
12
- if (check (window )) {
13
- return true ;
14
- }
15
- for (int i = n1 ; i < n2 ; i ++) {
16
- window [s2 .charAt (i ) - 'a' ]--;
17
- window [s2 .charAt (i - n1 ) - 'a' ]++;
18
- if (check (window )) {
19
- return true ;
20
- }
21
- }
22
- return false ;
23
- }
24
-
25
- private boolean check (int [] window ) {
26
- return Arrays .stream (window ).allMatch (cnt -> cnt == 0 );
27
- }
28
- }
1
+ class Solution {
2
+ public boolean checkInclusion (String s1 , String s2 ) {
3
+ int m = s1 .length ();
4
+ int n = s2 .length ();
5
+ if (m > n ) {
6
+ return false ;
7
+ }
8
+ int [] cnt = new int [26 ];
9
+ for (int i = 0 ; i < m ; ++i ) {
10
+ --cnt [s1 .charAt (i ) - 'a' ];
11
+ ++cnt [s2 .charAt (i ) - 'a' ];
12
+ }
13
+ int diff = 0 ;
14
+ for (int x : cnt ) {
15
+ if (x != 0 ) {
16
+ ++diff ;
17
+ }
18
+ }
19
+ if (diff == 0 ) {
20
+ return true ;
21
+ }
22
+ for (int i = m ; i < n ; ++i ) {
23
+ int a = s2 .charAt (i - m ) - 'a' ;
24
+ int b = s2 .charAt (i ) - 'a' ;
25
+ if (cnt [a ] == 0 ) {
26
+ ++diff ;
27
+ }
28
+ --cnt [a ];
29
+ if (cnt [a ] == 0 ) {
30
+ --diff ;
31
+ }
32
+ if (cnt [b ] == 0 ) {
33
+ ++diff ;
34
+ }
35
+ ++cnt [b ];
36
+ if (cnt [b ] == 0 ) {
37
+ --diff ;
38
+ }
39
+ if (diff == 0 ) {
40
+ return true ;
41
+ }
42
+ }
43
+ return false ;
44
+ }
45
+ }
Original file line number Diff line number Diff line change 1
- class Solution :
2
- def checkInclusion (self , s1 : str , s2 : str ) -> bool :
3
- n1 , n2 = len (s1 ), len (s2 )
4
- if n1 > n2 :
5
- return False
6
- window = [0 for _ in range (26 )]
7
- for i in range (n1 ):
8
- window [ord (s1 [i ]) - ord ('a' )] += 1
9
- window [ord (s2 [i ]) - ord ('a' )] -= 1
10
- if self .check (window ):
11
- return True
12
- for i in range (n1 , n2 ):
13
- window [ord (s2 [i ]) - ord ('a' )] -= 1
14
- window [ord (s2 [i - n1 ]) - ord ('a' )] += 1
15
- if self .check (window ):
16
- return True
17
- return False
18
-
19
- def check (self , window : List [int ]) -> bool :
20
- return all ([cnt == 0 for cnt in window ])
1
+ class Solution :
2
+ def checkInclusion (self , s1 : str , s2 : str ) -> bool :
3
+ m , n = len (s1 ), len (s2 )
4
+ if m > n :
5
+ return False
6
+ cnt = Counter ()
7
+ for a , b in zip (s1 , s2 ):
8
+ cnt [a ] -= 1
9
+ cnt [b ] += 1
10
+ diff = sum (x != 0 for x in cnt .values ())
11
+ if diff == 0 :
12
+ return True
13
+ for i in range (m , n ):
14
+ a , b = s2 [i - m ], s2 [i ]
15
+ if cnt [a ] == 0 :
16
+ diff += 1
17
+ cnt [a ] -= 1
18
+ if cnt [a ] == 0 :
19
+ diff -= 1
20
+ if cnt [b ] == 0 :
21
+ diff += 1
22
+ cnt [b ] += 1
23
+ if cnt [b ] == 0 :
24
+ diff -= 1
25
+ if diff == 0 :
26
+ return True
27
+ return False
Original file line number Diff line number Diff line change
1
+ function checkInclusion ( s1 : string , s2 : string ) : boolean {
2
+ const m = s1 . length ;
3
+ const n = s2 . length ;
4
+ if ( m > n ) {
5
+ return false ;
6
+ }
7
+ const cnt : number [ ] = new Array ( 26 ) . fill ( 0 ) ;
8
+ for ( let i = 0 ; i < m ; ++ i ) {
9
+ -- cnt [ s1 [ i ] . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ] ;
10
+ ++ cnt [ s2 [ i ] . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ] ;
11
+ }
12
+ let diff = 0 ;
13
+ for ( const x of cnt ) {
14
+ if ( x !== 0 ) {
15
+ ++ diff ;
16
+ }
17
+ }
18
+ if ( diff === 0 ) {
19
+ return true ;
20
+ }
21
+ for ( let i = m ; i < n ; ++ i ) {
22
+ const a = s2 [ i - m ] . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
23
+ const b = s2 [ i ] . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
24
+ if ( cnt [ a ] === 0 ) {
25
+ ++ diff ;
26
+ }
27
+ if ( -- cnt [ a ] === 0 ) {
28
+ -- diff ;
29
+ }
30
+ if ( cnt [ b ] === 0 ) {
31
+ ++ diff ;
32
+ }
33
+ if ( ++ cnt [ b ] === 0 ) {
34
+ -- diff ;
35
+ }
36
+ if ( diff === 0 ) {
37
+ return true ;
38
+ }
39
+ }
40
+ return false ;
41
+ }
You can’t perform that action at this time.
0 commit comments