Ctrl K

Roll Letters by Ranges

Medium
You are given a string word of lowercase English letters and a list of rolls rolls, where rolls[i] = [start, end, direction]. Each roll moves every letter from position start to position end, inclusive, by one letter: forward through the alphabet if direction = 1, backward if direction = 0. The alphabet wraps around: 'a' follows 'z', and 'z' comes before 'a'. Apply all the rolls and return the resulting string.
Example 1
word='cat', rolls=[[0, 2, 1], [1, 1, 0]] → 'dau'

First all three letters move forward: "dbu". Then the middle one moves back: "dau".

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

'a' moves back and wraps to 'z', while 'z' moves forward and wraps to 'a': "za".

Example 3
word='abc', rolls=[[0, 1, 1], [0, 1, 0]] → 'abc'

A forward and a backward roll over the same stretch cancel out, so the string stays the same.

Constraints
  • 1 ≤ word.length, rolls.length ≤ 5·10⁴
  • rolls[i].length == 3
  • 0 ≤ start ≤ end < word.length
  • direction is 0 or 1
  • word consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRoll Letters by Prefix MediumPalindrome After a Few Fixes MediumGather the Balls in Each Box MediumBooks Between Bookends MediumRecycling Truck Minutes Medium
loading Python…
Input
word='cat', rolls=[[0, 2, 1], [1, 1, 0]]
Expected
'dau'

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