Show problems as
Classic problem #2381MediumArrayStringPrefix Sum

Shifting Letters II

Real-world version: Rolling Letters Over Stretches
Tracked in this browser only
Read Shifting Letters II on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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