Skip to content

Commit 0ededa3

Browse files
committed
Sync LeetCode submission Runtime - 509 ms (46.56%), Memory - 20.5 MB (82.06%)
1 parent 655c462 commit 0ededa3

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>You are given a string <code>s</code> and a robot that currently holds an empty string <code>t</code>. Apply one of the following operations until <code>s</code> and <code>t</code> <strong>are both empty</strong>:</p>
2+
3+
<ul>
4+
<li>Remove the <strong>first</strong> character of a string <code>s</code> and give it to the robot. The robot will append this character to the string <code>t</code>.</li>
5+
<li>Remove the <strong>last</strong> character of a string <code>t</code> and give it to the robot. The robot will write this character on paper.</li>
6+
</ul>
7+
8+
<p>Return <em>the lexicographically smallest string that can be written on the paper.</em></p>
9+
10+
<p>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
<pre>
14+
<strong>Input:</strong> s = &quot;zza&quot;
15+
<strong>Output:</strong> &quot;azz&quot;
16+
<strong>Explanation:</strong> Let p denote the written string.
17+
Initially p=&quot;&quot;, s=&quot;zza&quot;, t=&quot;&quot;.
18+
Perform first operation three times p=&quot;&quot;, s=&quot;&quot;, t=&quot;zza&quot;.
19+
Perform second operation three times p=&quot;azz&quot;, s=&quot;&quot;, t=&quot;&quot;.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s = &quot;bac&quot;
26+
<strong>Output:</strong> &quot;abc&quot;
27+
<strong>Explanation:</strong> Let p denote the written string.
28+
Perform first operation twice p=&quot;&quot;, s=&quot;c&quot;, t=&quot;ba&quot;.
29+
Perform second operation twice p=&quot;ab&quot;, s=&quot;c&quot;, t=&quot;&quot;.
30+
Perform first operation p=&quot;ab&quot;, s=&quot;&quot;, t=&quot;c&quot;.
31+
Perform second operation p=&quot;abc&quot;, s=&quot;&quot;, t=&quot;&quot;.
32+
</pre>
33+
34+
<p><strong class="example">Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> s = &quot;bdda&quot;
38+
<strong>Output:</strong> &quot;addb&quot;
39+
<strong>Explanation:</strong> Let p denote the written string.
40+
Initially p=&quot;&quot;, s=&quot;bdda&quot;, t=&quot;&quot;.
41+
Perform first operation four times p=&quot;&quot;, s=&quot;&quot;, t=&quot;bdda&quot;.
42+
Perform second operation four times p=&quot;addb&quot;, s=&quot;&quot;, t=&quot;&quot;.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
50+
<li><code>s</code> consists of only English lowercase letters.</li>
51+
</ul>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Approach: Greedy + Stack
2+
3+
# Z = size of character set, n = len(s)
4+
# Time: O(n + Z)
5+
# Space: O(n)
6+
7+
from collections import Counter
8+
9+
class Solution:
10+
def robotWithString(self, s: str) -> str:
11+
cnt = Counter(s)
12+
stack = []
13+
res = []
14+
minChar = 'a'
15+
16+
for c in s:
17+
stack.append(c)
18+
cnt[c] -= 1
19+
while minChar != 'z' and cnt[minChar] == 0:
20+
minChar = chr(ord(minChar) + 1)
21+
22+
while stack and stack[-1] <= minChar:
23+
res.append(stack.pop())
24+
25+
return ''.join(res)
26+
27+

0 commit comments

Comments
 (0)