Perform String Shifts
Description
null
Solution(javascript)
/**
* @param {string} s
* @param {number[][]} shift
* @return {string}
*/
var stringShift = function(s, shift) {
const aux = (s,direction, count) => {
if(direction === 1) {
const pivot = s.length - count
const right = s.slice(pivot)
const left = s.slice(0, pivot)
return right + left
}
const pivot = count
const left = s.slice(0, pivot)
const right = s.slice(pivot)
return right + left
}
const count = shift.reduce((acc, [direction, count]) => direction === 0 ? acc - count : acc + count, 0)
return aux(s, count < 0 ? 0 : 1, Math.abs(count) % s.length)
};