Ctrl K

Roll Letters by Prefix

Medium
You are given a string word of lowercase English letters and an array rolls of the same length. Moving a letter means replacing it with the next one in the alphabet, with 'a' coming after 'z'. For each i, the value rolls[i] means the first i + 1 letters of the string are each moved rolls[i] times. Return the string you get after all the moves.
Example 1
word='dog', rolls=[1, 2, 3] → 'jtj'

The first letter is moved by all three rolls, 1 + 2 + 3 = 6, so 'd' → 'j'. The second by the last two, 5, so 'o' → 't'. The third by the last one only, 3, so 'g' → 'j'. The result is "jtj".

Example 2
word='zz', rolls=[1, 0] → 'az'

The first 'z' moves by 1 and wraps around to 'a', the second stays put: "az".

Example 3
word='m', rolls=[52] → 'm'

52 steps are exactly two full circles, so the letter does not change.

Constraints
  • 1 ≤ word.length ≤ 10⁵
  • word consists of lowercase English letters
  • rolls.length == word.length
  • 0 ≤ rolls[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPalindrome After a Few Fixes MediumGather the Balls in Each Box MediumBooks Between Bookends MediumRoll Letters by Ranges MediumRecycling Truck Minutes Medium
loading Python…
Input
word='dog', rolls=[1, 2, 3]
Expected
'jtj'

Run checks the first 3 tests in your browser. Submit checks all 81 tests on the server, hidden ones included.