Skip to content

Commit 6b9e001

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18 MB (12.86%)
1 parent ac41b3d commit 6b9e001

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<p>You are given a string <code>s</code> containing lowercase English letters, and a matrix <code>shift</code>, where <code>shift[i] = [direction<sub>i</sub>, amount<sub>i</sub>]</code>:</p>
2+
3+
<ul>
4+
<li><code>direction<sub>i</sub></code> can be <code>0</code> (for left shift) or <code>1</code> (for right shift).</li>
5+
<li><code>amount<sub>i</sub></code> is the amount by which string <code>s</code> is to be shifted.</li>
6+
<li>A left shift by 1 means remove the first character of <code>s</code> and append it to the end.</li>
7+
<li>Similarly, a right shift by 1 means remove the last character of <code>s</code> and add it to the beginning.</li>
8+
</ul>
9+
10+
<p>Return the final string after all operations.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> s = &quot;abc&quot;, shift = [[0,1],[1,2]]
17+
<strong>Output:</strong> &quot;cab&quot;
18+
<strong>Explanation:</strong>&nbsp;
19+
[0,1] means shift to left by 1. &quot;abc&quot; -&gt; &quot;bca&quot;
20+
[1,2] means shift to right by 2. &quot;bca&quot; -&gt; &quot;cab&quot;</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s = &quot;abcdefg&quot;, shift = [[1,1],[1,1],[0,2],[1,3]]
26+
<strong>Output:</strong> &quot;efgabcd&quot;
27+
<strong>Explanation:</strong>&nbsp;
28+
[1,1] means shift to right by 1. &quot;abcdefg&quot; -&gt; &quot;gabcdef&quot;
29+
[1,1] means shift to right by 1. &quot;gabcdef&quot; -&gt; &quot;fgabcde&quot;
30+
[0,2] means shift to left by 2. &quot;fgabcde&quot; -&gt; &quot;abcdefg&quot;
31+
[1,3] means shift to right by 3. &quot;abcdefg&quot; -&gt; &quot;efgabcd&quot;</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
38+
<li><code>s</code> only contains lower case English letters.</li>
39+
<li><code>1 &lt;= shift.length &lt;= 100</code></li>
40+
<li><code>shift[i].length == 2</code></li>
41+
<li><code>direction<sub>i</sub></code><sub> </sub>is either <code>0</code> or <code>1</code>.</li>
42+
<li><code>0 &lt;= amount<sub>i</sub> &lt;= 100</code></li>
43+
</ul>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Approach 2: Compute Net Shift
2+
3+
# L be the length of the string and N be the length of the shift array.
4+
# Time: O(N + L)
5+
# Space: O(L)
6+
7+
class Solution:
8+
def stringShift(self, s: str, shift: List[List[int]]) -> str:
9+
# Count the number of left shifts. A right shift is a negative left shift.
10+
left_shifts = 0
11+
12+
for direction, amount in shift:
13+
if direction == 1:
14+
amount = -amount
15+
left_shifts += amount
16+
17+
# Convert back to a positive, do left shifts, and return.
18+
left_shifts %= len(s)
19+
20+
s = s[left_shifts:] + s[:left_shifts]
21+
return s
22+

0 commit comments

Comments
 (0)