All problems
1089MediumArrayStringPrefix Sum

Rolling Letters Over Stretches

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2381Shifting Letters II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A tape of lowercase letters reads tape. Each entry rolls[i] = [from, to, forward] names a stretch of the tape from position from to position to, both ends included, and says which way every letter in that stretch rolls: 1 rolls each letter one step forward through the alphabet and 0 rolls it one step back.

The alphabet wraps round, so rolling 'z' forward gives 'a' and rolling 'a' back gives 'z'.

Apply every roll in the list and return the tape afterwards.

Examples

Example 1

Input
tape = "abc", rolls = [[0, 2, 1]]
Output
"bcd"

One roll forward over the whole tape moves each letter one step on.

Example 2

Input
tape = "az", rolls = [[0, 1, 1]]
Output
"ba"

Rolling both letters forward turns the a into b and wraps the z round to a.

Example 3

Input
tape = "abc", rolls = [[0, 0, 0]]
Output
"zbc"

Only the first letter is in the stretch, and rolling a backwards wraps it round to z.

Constraints

  • 1 <= tape.length <= 5 * 10^4
  • 1 <= rolls.length <= 5 * 10^4
  • rolls[i].length == 3
  • 0 <= rolls[i][0] <= rolls[i][1] <= tape.length - 1
  • 0 <= rolls[i][2] <= 1
  • The tape is made of lowercase English letters.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def shifting_letters(tape: str, rolls: list[list[int]]) -> str:
Java
public String shiftingLetters(String tape, int[][] rolls)
September 7
Apply