-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2381.py
40 lines (30 loc) · 1 KB
/
p2381.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
import unittest
from itertools import accumulate
from typing import List
class Solution:
@staticmethod
def shiftingLetters(s: str, shifts: List[List[int]]) -> str:
from string import ascii_lowercase
n = len(s)
diff = [0] * (n + 1)
for start, end, direction in shifts:
if direction == 1: # 向后
diff[start] += 1
diff[end + 1] -= 1
else: # 向前
diff[start] -= 1
diff[end + 1] += 1
ans = ""
for c, shift in zip(s, accumulate(diff[:-1])):
ans += ascii_lowercase[(ord(c) + shift - ord("a")) % 26]
return ans
class Test(unittest.TestCase):
def test(self) -> None:
self.assertEqual(
Solution.shiftingLetters("abc", [[0, 1, 0], [1, 2, 1], [0, 2, 1]]), "ace"
)
self.assertEqual(
Solution.shiftingLetters("dztz", [[0, 0, 0], [1, 1, 1]]), "catz"
)
if __name__ == "__main__":
unittest.main()