Skip to content

Commit 9e6d387

Browse files
committed
added stringRotation n-time n-space, working on n-time 1-space
1 parent ee6bb8d commit 9e6d387

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

chapter01/1.9 - String Rotation/stringRotation.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
1+
// var stringRotation = function(string1, string2) {
2+
// let counter = 0
3+
// let currStr = string1
4+
5+
// while (counter !== string1.length) {
6+
// // rotate string
7+
// // compare to string 2
8+
// // can only iterate the numer of times characters in string
9+
// currStr = currStr.slice(1) + currStr[0]
10+
// console.log(currStr, string2)
11+
// if (currStr === string2) return true
12+
// counter++
13+
// }
14+
15+
// return false
16+
// };
17+
118
var stringRotation = function(string1, string2) {
19+
let counter = 0
20+
let currStr = string1
21+
22+
while (counter !== string1.length) {
23+
// rotate string
24+
// compare to string 2
25+
// can only iterate the numer of times characters in string
26+
currStr = currStr.slice(1) + currStr[0]
27+
console.log(currStr, string2)
28+
if (currStr === string2) return true
29+
counter++
30+
}
231

32+
return false
333
};
434

535
// Approaches:

0 commit comments

Comments
 (0)