mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
52 lines
2.3 KiB
HTML
52 lines
2.3 KiB
HTML
|
<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>
|
||
|
|
||
|
<ul>
|
||
|
<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>
|
||
|
<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>
|
||
|
</ul>
|
||
|
|
||
|
<p>Return <em>the lexicographically smallest string that can be written on the paper.</em></p>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong class="example">Example 1:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> s = "zza"
|
||
|
<strong>Output:</strong> "azz"
|
||
|
<strong>Explanation:</strong> Let p denote the written string.
|
||
|
Initially p="", s="zza", t="".
|
||
|
Perform first operation three times p="", s="", t="zza".
|
||
|
Perform second operation three times p="azz", s="", t="".
|
||
|
</pre>
|
||
|
|
||
|
<p><strong class="example">Example 2:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> s = "bac"
|
||
|
<strong>Output:</strong> "abc"
|
||
|
<strong>Explanation:</strong> Let p denote the written string.
|
||
|
Perform first operation twice p="", s="c", t="ba".
|
||
|
Perform second operation twice p="ab", s="c", t="".
|
||
|
Perform first operation p="ab", s="", t="c".
|
||
|
Perform second operation p="abc", s="", t="".
|
||
|
</pre>
|
||
|
|
||
|
<p><strong class="example">Example 3:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> s = "bdda"
|
||
|
<strong>Output:</strong> "addb"
|
||
|
<strong>Explanation:</strong> Let p denote the written string.
|
||
|
Initially p="", s="bdda", t="".
|
||
|
Perform first operation four times p="", s="", t="bdda".
|
||
|
Perform second operation four times p="addb", s="", t="".
|
||
|
</pre>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||
|
<li><code>s</code> consists of only English lowercase letters.</li>
|
||
|
</ul>
|