Skip to content

Commit 1464db0

Browse files
committed
Jan 5
1 parent f25b6fe commit 1464db0

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

2025-01-January-LeetCoding-Challenge/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| January 2 | [2559. Count Vowel Strings in Ranges](https://leetcode.com/problems/count-vowel-strings-in-ranges/) | Medium | Solved |
88
| January 3 | [2270. Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | Medium | Solved |
99
| January 4 | [1930. Unique Length-3 Palindromic Subsequences](https://leetcode.com/problems/unique-length-3-palindromic-subsequences/) | Medium | Solved |
10-
| January 5 | []() | | |
10+
| January 5 | [2381. Shifting Letters II](https://leetcode.com/problems/shifting-letters-ii/) | Medium | Solved |
1111
| January 6 | []() | | |
1212
| January 7 | []() | | |
1313
| January 8 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 1 | 1 | 0 |
43-
| Medium | 3 | 3 | 0 |
43+
| Medium | 4 | 4 | 0 |
4444
| Hard | 0 | 0 | 0 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from itertools import accumulate
2+
from typing import List
3+
4+
5+
class Solution:
6+
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
7+
# line sweep algorithm
8+
line = [0] * (len(s) + 1) # account for shift after last element
9+
for start, end, direction in shifts:
10+
if direction == 1:
11+
# start shifting from start
12+
line[start] += 1
13+
# stop shifting after end
14+
line[end + 1] -= 1
15+
else:
16+
line[start] -= 1
17+
line[end + 1] += 1
18+
# prefix sum to calculate total shifts
19+
line = accumulate(line)
20+
21+
result = ''
22+
for ch, sh in zip(s, line):
23+
result += chr(ord('a') + ((ord(ch) - ord('a') + sh) % 26))
24+
return result
25+
26+
27+
def main():
28+
s = 'abc'
29+
shifts = [[0, 1, 0], [1, 2, 1], [0, 2, 1]]
30+
assert Solution().shiftingLetters(s, shifts) == 'ace'
31+
32+
s = 'dztz'
33+
shifts = [[0, 0, 0], [1, 1, 1]]
34+
assert Solution().shiftingLetters(s, shifts) == 'catz'
35+
36+
37+
if __name__ == '__main__':
38+
main()

0 commit comments

Comments
 (0)