DEV Community

Discussion on: 30-Day LeetCoding Challenge: Perform String Shifts

Collapse
 
tikenov profile image
Azamat

Hi, I think it will be better, to sum the array of shifts. so the left shift is "-" and right is "+".
Finally, just make a single shift operation. Here is my JS solution, hope this makes sense:

var stringShift = function(s, shift) {
    let i = shift.map(e=>(e[0]?1:-1)*e[1]).reduce((a,e)=>a+e)%s.length;
    i = i > 0 ? s.length-i : i*-1;
    return s.substring(i)+s.substring(0,i);
};
Collapse
 
luffy_14 profile image
Ahmad Ra'fat

Yes, that is also a very good approach 👏👏