Shifting Letters II

You are given a string s of lowercase English letters and a 2D integer array shifts where shifts[i] = [starti, endi, directioni]. For every i, shift the characters in s from the index starti to the index endi (inclusive) forward if directioni = 1, or shift the characters backward if directioni = 0.

Shifting a character forward means replacing it with the next letter in the alphabet, wrapping around so that 'z' becomes 'a'. Similarly, shifting a character backward means replacing it with the previous letter in the alphabet, wrapping around so that 'a' becomes 'z'.

Return the final string after all such shifts to s are applied.

Example 1
Inputs = "abc", shifts = [[0,1,0],[1,2,1],[0,2,1]]
Output"ace"
Firstly, shifting indices 0 to 1 backward makes s = "zac", then shifting indices 1 to 2 forward makes s = "zbd", and finally shifting indices 0 to 2 forward makes s = "ace".
Example 2
Inputs = "dztz", shifts = [[0,0,0],[1,1,1]]
Output"catz"
Shifting index 0 backward makes s = "cztz", and shifting index 1 forward makes s = "catz".

Constraints

  • 1 <= s.length, shifts.length <= 5 * 10^4
  • shifts[i].length == 3
  • 0 <= starti <= endi < s.length
  • 0 <= directioni <= 1
  • s consists of lowercase English letters.

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate