|
| 1 | +<h2><a href="https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/?envType=daily-question&envId=2025-06-06">2434. Using a Robot to Print the Lexicographically Smallest String</a></h2><h3>Medium</h3><hr><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> </p> |
| 11 | +<p><strong class="example">Example 1:</strong></p> |
| 12 | + |
| 13 | +<pre> |
| 14 | +<strong>Input:</strong> s = "zza" |
| 15 | +<strong>Output:</strong> "azz" |
| 16 | +<strong>Explanation:</strong> Let p denote the written string. |
| 17 | +Initially p="", s="zza", t="". |
| 18 | +Perform first operation three times p="", s="", t="zza". |
| 19 | +Perform second operation three times p="azz", s="", t="". |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong class="example">Example 2:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> s = "bac" |
| 26 | +<strong>Output:</strong> "abc" |
| 27 | +<strong>Explanation:</strong> Let p denote the written string. |
| 28 | +Perform first operation twice p="", s="c", t="ba". |
| 29 | +Perform second operation twice p="ab", s="c", t="". |
| 30 | +Perform first operation p="ab", s="", t="c". |
| 31 | +Perform second operation p="abc", s="", t="". |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong class="example">Example 3:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input:</strong> s = "bdda" |
| 38 | +<strong>Output:</strong> "addb" |
| 39 | +<strong>Explanation:</strong> Let p denote the written string. |
| 40 | +Initially p="", s="bdda", t="". |
| 41 | +Perform first operation four times p="", s="", t="bdda". |
| 42 | +Perform second operation four times p="addb", s="", t="". |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= s.length <= 10<sup>5</sup></code></li> |
| 50 | + <li><code>s</code> consists of only English lowercase letters.</li> |
| 51 | +</ul> |
0 commit comments