Created
April 14, 2020 16:11
-
-
Save drewkiimon/b6d936d730dc94ca71435c82f2f82f88 to your computer and use it in GitHub Desktop.
Leetcode 30 Day Challenge - Day 14
This file contains 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
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