Skip to content

Instantly share code, notes, and snippets.

@vishnuroshan
Last active March 20, 2024 14:33
Show Gist options
  • Save vishnuroshan/9a75127ffaab6f29b6e8160d927b8e48 to your computer and use it in GitHub Desktop.
Save vishnuroshan/9a75127ffaab6f29b6e8160d927b8e48 to your computer and use it in GitHub Desktop.
Find number of rotations needed for the strings to match
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)
@vishnuroshan
Copy link
Author

this was a question asked in an interview with ZeMoSo labs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment