Shifting Letters II
Real-world version: Rolling Letters Over StretchesThe 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.
def shifting_letters(tape: str, rolls: list[list[int]]) -> str:public String shiftingLetters(String tape, int[][] rolls)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Count Vowel Strings in RangesReal-world version: Vowel-Bounded Labels in Each StretchMedium
- Minimum Number of Operations to Move All Balls to Each BoxReal-world version: Gathering the Crates Into Every BayMedium
- Longest Common PrefixReal-world version: Shared Crate CodeEasy
- Find Pivot IndexReal-world version: Beam Balance StationEasy