#2381
Shifting Letters II
specialist · 815 · lc medium +31 · verified · 53.5% accepted · 1,762 likes · top 45%
Description
You are given a lowercase string s and a 2D array shifts where each entry shifts[i] = [starti, endi, directioni] instructs you to shift every character of s from index starti to endi inclusive: forward (toward 'z') if directioni = 1, or backward (toward 'a') if directioni = 0. Shifts wrap around the alphabet.
Apply all shift operations and return the resulting string.
Example 1:
Input: s = "abc", shifts = [[0,1,0],[1,2,1],[0,2,1]]
Output: "ace"
Explanation: Firstly, shift the characters from index 0 to index 1 backward. Now s = "zac".
Secondly, shift the characters from index 1 to index 2 forward. Now s = "zbd".
Finally, shift the characters from index 0 to index 2 forward. Now s = "ace".
Example 2:
Input: s = "dztz", shifts = [[0,0,0],[1,1,1]]
Output: "catz"
Explanation: Firstly, shift the characters from index 0 to index 0 backward. Now s = "cztz".
Finally, shift the characters from index 1 to index 1 forward. Now s = "catz".
Code
1
2
3