Last active
January 5, 2025 23:20
-
-
Save doyedele1/d4ecb6b7be6f81d3b215cb9cd5ad702e to your computer and use it in GitHub Desktop.
2381. Shifting Letters II
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str: | |
n = len(s) | |
arr = [0] * (n + 1) | |
for shift in shifts: | |
start, end, direction = shift[0], shift[1], shift[2] | |
if direction == 1: | |
arr[start] += 1 | |
arr[end + 1] -= 1 | |
else: | |
arr[start] -= 1 | |
arr[end + 1] += 1 | |
for i in range(1, n + 1): | |
arr[i] += arr[i - 1] | |
res = [] | |
for i in range(n): | |
shift = arr[i] % 26 | |
if shift < 0: | |
shift += 26 # just here to ensure non-negative shift | |
new_char = chr((ord(s[i]) - ord('a') + shift) % 26 + ord('a')) | |
res.append(new_char) | |
return ''.join(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment