Last active
March 20, 2024 14:33
-
-
Save vishnuroshan/9a75127ffaab6f29b6e8160d927b8e48 to your computer and use it in GitHub Desktop.
Find number of rotations needed for the strings to match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cycle(a, b) { | |
if (a.length !== b.length) return 0; | |
const arr = a.split(''); | |
const temp = [...arr]; // i am copying the array. | |
let count = 0; | |
for(let e of arr) { | |
count += 1; | |
console.log(temp.join('')); | |
const firstElement = temp.shift(); // this removes the 1st element of the array | |
temp.push(firstElement); | |
if (temp.join('') === b) { | |
break; | |
} | |
} | |
return count; | |
} | |
const result = cycle('chocolate', 'olatechoc'); | |
console.log(result) |
Author
vishnuroshan
commented
Mar 20, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment