File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
LeetCode/567. Permutation in String Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ var isPermutation = function ( s1 , s2 ) {
2+ if ( s1 . length !== s2 . length ) return false ;
3+
4+ let hashMapOfS1 = { } , hashMapOfS2 = { } ;
5+ for ( let i = 0 ; i < s1 . length ; i ++ ) {
6+ hashMapOfS1 [ s1 [ i ] ] = hashMapOfS1 [ s1 [ i ] ] + 1 || 1 ;
7+ }
8+ for ( let i = 0 ; i < s2 . length ; i ++ ) {
9+ hashMapOfS2 [ s2 [ i ] ] = hashMapOfS2 [ s2 [ i ] ] + 1 || 1 ;
10+ }
11+
12+ for ( keys in hashMapOfS1 ) {
13+ if ( hashMapOfS1 [ keys ] !== hashMapOfS2 [ keys ] ) return false ;
14+ }
15+
16+ return true ;
17+ }
18+
19+ var checkInclusion = function ( s1 , s2 ) {
20+ let permutationLength = s1 . length ;
21+ let first = 0 , second = permutationLength - 1 ;
22+ while ( first < s2 . length && second < s2 . length ) {
23+ let currentStr = s2 . slice ( first , second + 1 ) ;
24+ if ( isPermutation ( s1 , currentStr ) ) return true ;
25+ first ++ ;
26+ second ++ ;
27+ }
28+ return false ;
29+ } ;
You can’t perform that action at this time.
0 commit comments