File tree 1 file changed +9
-8
lines changed
scripts/algorithms/P/Permutation in String
1 file changed +9
-8
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 135 ms (Top 60.53%) | Memory: 45.4 MB (Top 55.95%)
1
2
const getCharIdx = ( c ) => c . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
2
3
const isEqual = ( a , b ) => a . every ( ( v , i ) => v == b [ i ] ) ;
3
4
4
5
var checkInclusion = function ( s1 , s2 ) {
5
6
const occS1 = new Array ( 26 ) . fill ( 0 ) ;
6
7
const occS2 = new Array ( 26 ) . fill ( 0 ) ;
7
-
8
+
8
9
const s1Len = s1 . length , s2Len = s2 . length ;
9
-
10
+
10
11
if ( s1Len > s2Len ) return false ;
11
-
12
+
12
13
let l = 0 , r = 0 ;
13
14
for ( ; r < s1Len ; r ++ ) {
14
15
occS1 [ getCharIdx ( s1 [ r ] ) ] ++ ;
15
16
occS2 [ getCharIdx ( s2 [ r ] ) ] ++ ;
16
17
}
17
-
18
+
18
19
if ( isEqual ( occS1 , occS2 ) ) {
19
20
return true ;
20
21
}
21
-
22
+
22
23
for ( ; r < s2Len ; r ++ ) {
23
24
occS2 [ getCharIdx ( s2 [ r ] ) ] ++ ;
24
25
occS2 [ getCharIdx ( s2 [ l ++ ] ) ] -- ;
25
-
26
+
26
27
if ( isEqual ( occS1 , occS2 ) ) return true ;
27
28
}
28
-
29
+
29
30
return false ;
30
- } ;
31
+ } ;
You can’t perform that action at this time.
0 commit comments