Leetcode•Nov 20, 2026

Shifting Letters

Hazrat Ali

Leetcode

You are given a string s of lowercase English letters and an integer array shifts of the same length.

Call the shift() of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a').

  • For example, shift('a') = 'b', shift('t') = 'u', and shift('z') = 'a'.

Now for each shifts[i] = x, we want to shift the first i + 1 letters of s, x times.

Return the final string after all such shifts to s are applied.

 

Example 1:

Input: s = "abc", shifts = [3,5,9]
Output: "rpl"
Explanation: We start with "abc".
After shifting the first 1 letters of s by 3, we have "dbc".
After shifting the first 2 letters of s by 5, we have "igc".
After shifting the first 3 letters of s by 9, we have "rpl", the answer.

Example 2:

Input: s = "aaa", shifts = [1,2,3]
Output: "gfd"

Solution
var shiftingLetters = function(s, shifts) {
    let totalShift = 0;
    let result = s.split("");

    for (let i = s.length - 1; i >= 0; i--) {
        totalShift = (totalShift + shifts[i]) % 26;

        let charCode = s.charCodeAt(i) - 97;
        let newChar = (charCode + totalShift) % 26;

        result[i] = String.fromCharCode(newChar + 97);
    }

    return result.join("");
};
 

Comments