1+ // O (n)
12function isStringRotation ( s1 , s2 ) {
3+ return isSubstring ( s1 + s1 , s2 ) ;
4+ }
5+
6+ function isSubstring ( s1 , s2 ) {
7+ return s1 . indexOf ( s2 ) > - 1 || s2 . indexOf ( s1 ) > - 1 ;
8+ }
9+
10+ console . log ( isStringRotation ( 'ab' , 'ab' ) ) ;
11+ console . log ( isStringRotation ( 'ba' , 'ab' ) ) ;
12+
13+ console . log ( isStringRotation ( 'aab' , 'aab' ) ) ;
14+ console . log ( isStringRotation ( 'aba' , 'aab' ) ) ;
15+ console . log ( isStringRotation ( 'baa' , 'aab' ) ) ;
16+
17+
18+ console . log ( isStringRotation ( 'aaaab' , 'aaaab' ) ) ;
19+ console . log ( isStringRotation ( 'aaaba' , 'aaaab' ) ) ;
20+ console . log ( isStringRotation ( 'aabaa' , 'aaaab' ) ) ;
21+ console . log ( isStringRotation ( 'abaaa' , 'aaaab' ) ) ;
22+ console . log ( isStringRotation ( 'baaaa' , 'aaaab' ) ) ;
23+
24+ console . log ( isStringRotation ( 'waterbottle' , 'bottlewater' ) ) ;
25+
26+ console . log ( isStringRotation ( 'waterbotlle' , 'bottlewater' ) ) ; // false
27+
28+ // O (n^2)
29+ function isStringRotationSlow ( s1 , s2 ) {
230 if ( s1 . length !== s2 . length ) {
331 return false ;
432 }
@@ -21,24 +49,4 @@ function isStringRotation(s1, s2) {
2149 }
2250 }
2351 return false ;
24- }
25-
26- function isSubstring ( s1 , s2 ) {
27- return s1 . indexOf ( s2 ) > - 1 || s2 . indexOf ( s1 ) > - 1 ;
28- }
29-
30- console . log ( isStringRotation ( 'ab' , 'ab' ) ) ;
31- console . log ( isStringRotation ( 'ba' , 'ab' ) ) ;
32-
33- console . log ( isStringRotation ( 'aab' , 'aab' ) ) ;
34- console . log ( isStringRotation ( 'aba' , 'aab' ) ) ;
35- console . log ( isStringRotation ( 'baa' , 'aab' ) ) ;
36-
37-
38- console . log ( isStringRotation ( 'aaaab' , 'aaaab' ) ) ;
39- console . log ( isStringRotation ( 'aaaba' , 'aaaab' ) ) ;
40- console . log ( isStringRotation ( 'aabaa' , 'aaaab' ) ) ;
41- console . log ( isStringRotation ( 'abaaa' , 'aaaab' ) ) ;
42- console . log ( isStringRotation ( 'baaaa' , 'aaaab' ) ) ;
43-
44- console . log ( isStringRotation ( 'waterbottle' , 'bottlewater' ) ) ;
52+ }
0 commit comments