@@ -30,51 +30,51 @@ The substring with start index = 2 is "ab", which is an anagram of "ab".
30
30
* @param {string } p
31
31
* @return {number[] }
32
32
*/
33
- var findAnagrams = function ( s , p ) {
34
- if ( s . length < p . length ) { return [ ] }
33
+ var findAnagrams = function ( s , p ) {
34
+ if ( s . length < p . length ) { return [ ] ; }
35
35
36
36
let start = 0 ;
37
37
let end = p . length - 1 ;
38
38
let hashBuild = { } ;
39
39
let countLeft = p . length ;
40
- let anagrams = [ ]
40
+ let anagrams = [ ] ;
41
41
42
42
for ( let e = 0 ; e < p . length ; e ++ ) {
43
- hashBuild [ p [ e ] ] = hashBuild [ p [ e ] ] !== undefined ? hashBuild [ p [ e ] ] + 1 : 1 ;
43
+ hashBuild [ p [ e ] ] = hashBuild [ p [ e ] ] !== undefined ? hashBuild [ p [ e ] ] + 1 : 1 ;
44
44
}
45
45
46
46
for ( let i = start ; i < end ; i ++ ) {
47
- if ( hashBuild [ s [ i ] ] !== undefined ) {
48
- hashBuild [ s [ i ] ] = hashBuild [ s [ i ] ] - 1 ;
49
- if ( hashBuild [ s [ i ] ] >= 0 ) {
50
- countLeft -- ;
51
- }
47
+ if ( hashBuild [ s [ i ] ] !== undefined ) {
48
+ hashBuild [ s [ i ] ] = hashBuild [ s [ i ] ] - 1 ;
49
+ if ( hashBuild [ s [ i ] ] >= 0 ) {
50
+ countLeft -- ;
52
51
}
52
+ }
53
53
}
54
54
55
55
while ( end < s . length ) {
56
- // check left
57
- if ( hashBuild [ s [ end ] ] !== undefined ) {
58
- hashBuild [ s [ end ] ] = hashBuild [ s [ end ] ] - 1 ;
59
- if ( hashBuild [ s [ end ] ] >= 0 ) {
60
- countLeft -- ;
61
- }
62
- if ( countLeft == 0 ) {
63
- anagrams . push ( start ) ;
64
- }
56
+ // check left
57
+ if ( hashBuild [ s [ end ] ] !== undefined ) {
58
+ hashBuild [ s [ end ] ] = hashBuild [ s [ end ] ] - 1 ;
59
+ if ( hashBuild [ s [ end ] ] >= 0 ) {
60
+ countLeft -- ;
65
61
}
62
+ if ( countLeft == 0 ) {
63
+ anagrams . push ( start ) ;
64
+ }
65
+ }
66
66
67
- // check right
68
- if ( hashBuild [ s [ start ] ] !== undefined ) {
69
- hashBuild [ s [ start ] ] = hashBuild [ s [ start ] ] + 1 ;
70
- if ( hashBuild [ s [ start ] ] >= 1 ) {
71
- countLeft ++ ;
72
- }
67
+ // check right
68
+ if ( hashBuild [ s [ start ] ] !== undefined ) {
69
+ hashBuild [ s [ start ] ] = hashBuild [ s [ start ] ] + 1 ;
70
+ if ( hashBuild [ s [ start ] ] >= 1 ) {
71
+ countLeft ++ ;
73
72
}
73
+ }
74
74
75
- // slide window
76
- end ++ ;
77
- start ++ ;
75
+ // slide window
76
+ end ++ ;
77
+ start ++ ;
78
78
}
79
79
80
80
return anagrams ;
0 commit comments