Shifting Letters - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: s = "abc", shifts = [3,5,9]
Output: "rpl"
💡 Note: Position 0 ('a') gets shifted 3+5+9=17 times: a→r. Position 1 ('b') gets shifted 5+9=14 times: b→p. Position 2 ('c') gets shifted 9 times: c→l.
Example 2 — Single Character
$ Input: s = "z", shifts = [25]
Output: "y"
💡 Note: The character 'z' is shifted 25 times. Since z+25 wraps around, z→y (25 shifts from z brings us to y).
Example 3 — Wrapping Around
$ Input: s = "aaa", shifts = [1,2,3]
Output: "gfd"
💡 Note: Position 0: a + (1+2+3)=6 shifts → g. Position 1: a + (2+3)=5 shifts → f. Position 2: a + 3 shifts → d.

Constraints

  • 1 ≤ s.length ≤ 105
  • s consists of lowercase English letters
  • shifts.length == s.length
  • 0 ≤ shifts[i] ≤ 109

Visualization

Tap to expand
Shifting Letters: How Operations AccumulateInput String:abcShifts Array:359Total Shifts:3+5+9=175+9=149Final Result:rplEach position accumulates all shifts from its index onward
Understanding the Visualization
1
Input
String 'abc' and shifts [3,5,9]
2
Calculate Shifts
Each position gets cumulative shifts from its index onward
3
Output
Apply total shifts to get final string
Key Takeaway
🎯 Key Insight: Work backwards to efficiently calculate cumulative effects
Asked in
Google 15 Facebook 12
89.7K Views
Medium Frequency
~15 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen