This commit is contained in:
parent
51e5a1a04e
commit
018bef7505
1 changed files with 29 additions and 0 deletions
29
medium/shifting_letters_2.py
Normal file
29
medium/shifting_letters_2.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# https://leetcode.com/problems/shifting-letters-ii
|
||||
|
||||
from typing import List
|
||||
|
||||
class Solution:
|
||||
def convert(self, s: str, shift: int) -> str:
|
||||
return chr(
|
||||
(
|
||||
(ord(s) - ord('a') + shift) % 26
|
||||
) + ord('a')
|
||||
)
|
||||
|
||||
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
|
||||
l = len(s)
|
||||
shifting = [0] * (l + 1)
|
||||
for [start, end, direction] in shifts:
|
||||
if direction == 0:
|
||||
shifting[start] -= 1
|
||||
shifting[end + 1] += 1
|
||||
else:
|
||||
shifting[start] += 1
|
||||
shifting[end + 1] -= 1
|
||||
shift = 0
|
||||
result = []
|
||||
for i in range(len(s)):
|
||||
shift += shifting[i]
|
||||
result.append(self.convert(s[i], shift))
|
||||
return ''.join(result)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue