Skip to content

Instantly share code, notes, and snippets.

@drewkiimon
Created April 14, 2020 16:11
Show Gist options
  • Save drewkiimon/b6d936d730dc94ca71435c82f2f82f88 to your computer and use it in GitHub Desktop.
Save drewkiimon/b6d936d730dc94ca71435c82f2f82f88 to your computer and use it in GitHub Desktop.
Leetcode 30 Day Challenge - Day 14
var stringShift = function(s, shift) {
var move = 0,
s= s.split('');
for (var i = 0; i < shift.length; i ++) {
if (shift[i][0] === 0) {
move -= shift[i][1];
} else if (shift[i][0] === 1) {
move += shift[i][1];
}
}
if (move === 0 || move % s.length === 0) return s.join('');
else if (move < 0) {
// left
let moves = (move * -1) % s.length;
for (var i = 0; i < moves; i ++) {
var letter = s.shift();
s.push(letter);
}
} else if (move > 0) {
// right
let moves = (move) % s.length;
for (var i = 0; i < moves; i ++) {
var letter = s.pop();
s.unshift(letter);
}
}
return s.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment