Shifting Letters II

Medium Topics Company Tags

You are given a lowercase string s and operations in shifts. Each operation [start, end, direction] changes every character in the inclusive range from start to end by one alphabet position.

A direction of 1 moves forward, with z wrapping to a. A direction of 0 moves backward, with a wrapping to z. Apply all operations and return the final string.

Example 1:

Input: s = "az"
shifts = [[0,1,1]]

Output: "ba"

Explanation: Shift both characters forward once: a becomes b, and z wraps around to a. The result is "ba".

Example 2:

Input: s = "aaa"
shifts = [[0,2,0],[1,1,1]]

Output: "zaz"

Explanation: The first operation shifts all three a characters backward, producing "zzz". The second shifts only the middle character forward, so the final string is "zaz".

Example 3:

Input: s = "xyz"
shifts = [[0,1,1],[1,2,0]]

Output: "yyy"

Explanation: The first operation changes "xyz" to "yzz". The second moves the last two characters backward, resulting in "yyy". The shifts cancel at the overlapping middle position.

Constraints:

  • 1 <= s.length <= 50000
  • 1 <= shifts.length <= 50000
  • s contains only lowercase English letters.
  • Each shift contains exactly three integers: [start, end, direction].
  • 0 <= start <= end < s.length
  • direction is either 0 or 1.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.