Skip to content

Commit b916b3c

Browse files
committed
Sync LeetCode submission Runtime - 67 ms (45.05%), Memory - 41.2 MB (49.85%)
1 parent d3bc6bd commit b916b3c

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

2465-shifting-letters-ii/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<p>You are given a string <code>s</code> of lowercase English letters and a 2D integer array <code>shifts</code> where <code>shifts[i] = [start<sub>i</sub>, end<sub>i</sub>, direction<sub>i</sub>]</code>. For every <code>i</code>, <strong>shift</strong> the characters in <code>s</code> from the index <code>start<sub>i</sub></code> to the index <code>end<sub>i</sub></code> (<strong>inclusive</strong>) forward if <code>direction<sub>i</sub> = 1</code>, or shift the characters backward if <code>direction<sub>i</sub> = 0</code>.</p>
2+
3+
<p>Shifting a character <strong>forward</strong> means replacing it with the <strong>next</strong> letter in the alphabet (wrapping around so that <code>&#39;z&#39;</code> becomes <code>&#39;a&#39;</code>). Similarly, shifting a character <strong>backward</strong> means replacing it with the <strong>previous</strong> letter in the alphabet (wrapping around so that <code>&#39;a&#39;</code> becomes <code>&#39;z&#39;</code>).</p>
4+
5+
<p>Return <em>the final string after all such shifts to </em><code>s</code><em> are applied</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> s = &quot;abc&quot;, shifts = [[0,1,0],[1,2,1],[0,2,1]]
12+
<strong>Output:</strong> &quot;ace&quot;
13+
<strong>Explanation:</strong> Firstly, shift the characters from index 0 to index 1 backward. Now s = &quot;zac&quot;.
14+
Secondly, shift the characters from index 1 to index 2 forward. Now s = &quot;zbd&quot;.
15+
Finally, shift the characters from index 0 to index 2 forward. Now s = &quot;ace&quot;.</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> s = &quot;dztz&quot;, shifts = [[0,0,0],[1,1,1]]
21+
<strong>Output:</strong> &quot;catz&quot;
22+
<strong>Explanation:</strong> Firstly, shift the characters from index 0 to index 0 backward. Now s = &quot;cztz&quot;.
23+
Finally, shift the characters from index 1 to index 1 forward. Now s = &quot;catz&quot;.
24+
</pre>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>1 &lt;= s.length, shifts.length &lt;= 5 * 10<sup>4</sup></code></li>
31+
<li><code>shifts[i].length == 3</code></li>
32+
<li><code>0 &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt; s.length</code></li>
33+
<li><code>0 &lt;= direction<sub>i</sub> &lt;= 1</code></li>
34+
<li><code>s</code> consists of lowercase English letters.</li>
35+
</ul>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Approach: Cumulative Shift
2+
3+
# n = length of shifts array, m = length of string
4+
# Time: O(n + m)
5+
# Space: O(m)
6+
7+
class Solution:
8+
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
9+
n = len(s)
10+
# Create array to track cumulative shifts for each position
11+
shift_count = [0] * n
12+
13+
for start, end, direction in shifts:
14+
# Add 1 for forward shift, -1 for backward shift
15+
delta = 1 if direction == 1 else -1
16+
shift_count[start] += delta
17+
18+
if end + 1 < n: # Cancel out effect after end position
19+
shift_count[end + 1] -= delta
20+
21+
# Calculate prefix sum to get total shifts for each position
22+
for i in range(1, n):
23+
shift_count[i] += shift_count[i - 1]
24+
25+
# Apply shifts all at once using modulo arithmetic
26+
result = []
27+
for i in range(n):
28+
# Calculate new character position
29+
new_pos = (ord(s[i]) - ord('a') + shift_count[i]) % 26
30+
result.append(chr(ord('a') + new_pos))
31+
32+
return ''.join(result)
33+

0 commit comments

Comments
 (0)