-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2381-shifting-letters-ii.py
48 lines (35 loc) · 1.43 KB
/
2381-shifting-letters-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from typing import List
import heapq
def shift(char, amount):
return chr((ord(char) + amount - 97) % 26 + 97)
class Solution:
def shiftingLetterNaive(self, s: str, shifts: List[List[int]]) -> str:
shifts.sort()
next_shift = 0
current_shifts = []
consolidated_shift = 0
answer = [c for c in s]
for i, c in enumerate(s):
while next_shift < len(shifts) and i == shifts[next_shift][0]:
_, end, direction = shifts[next_shift]
amount = -1 if direction == 0 else 1
next_shift += 1
consolidated_shift += amount
heapq.heappush(current_shifts, (end, amount))
answer[i] = shift(c, consolidated_shift)
while len(current_shifts) > 0 and current_shifts[0][0] == i:
_, amount = heapq.heappop(current_shifts)
consolidated_shift -= amount
return "".join(answer)
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
changes = [0] * (len(s) + 1)
for start, end, direction in shifts:
change = -1 if direction == 0 else 1
changes[start] += change
changes[end + 1] -= change
current_shift = 0
answer = [None for c in s]
for i, c in enumerate(s):
current_shift += changes[i]
answer[i] = shift(c, current_shift)
return "".join(answer)