java.lang.Object
g2401_2500.s2434_using_a_robot_to_print_the_lexicographically_smallest_string.Solution

public class Solution extends Object
2434 - Using a Robot to Print the Lexicographically Smallest String.<p>Medium</p> <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><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;zza&rdquo;</p> <p><strong>Output:</strong> &ldquo;azz&rdquo;</p> <p><strong>Explanation:</strong> Let p denote the written string.</p> <p>Initially p=&ldquo;&ldquo;, s=&ldquo;zza&rdquo;, t=&rdquo;&rdquo;.</p> <p>Perform first operation three times p=&ldquo;&ldquo;, s=&rdquo;&rdquo;, t=&ldquo;zza&rdquo;.</p> <p>Perform second operation three times p=&ldquo;azz&rdquo;, s=&ldquo;&ldquo;, t=&rdquo;&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;bac&rdquo;</p> <p><strong>Output:</strong> &ldquo;abc&rdquo;</p> <p><strong>Explanation:</strong> Let p denote the written string.</p> <p>Perform first operation twice p=&quot;&quot;, s=&ldquo;c&rdquo;, t=&ldquo;ba&rdquo;.</p> <p>Perform second operation twice p=&ldquo;ab&rdquo;, s=&ldquo;c&rdquo;, t=&quot;&quot;.</p> <p>Perform first operation p=&ldquo;ab&rdquo;, s=&quot;&quot;, t=&ldquo;c&rdquo;.</p> <p>Perform second operation p=&ldquo;abc&rdquo;, s=&ldquo;&ldquo;, t=&rdquo;&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;bdda&rdquo;</p> <p><strong>Output:</strong> &ldquo;addb&rdquo;</p> <p><strong>Explanation:</strong> Let p denote the written string.</p> <p>Initially p=&ldquo;&ldquo;, s=&ldquo;bdda&rdquo;, t=&rdquo;&rdquo;.</p> <p>Perform first operation four times p=&ldquo;&ldquo;, s=&rdquo;&rdquo;, t=&ldquo;bdda&rdquo;.</p> <p>Perform second operation four times p=&ldquo;addb&rdquo;, s=&ldquo;&ldquo;, t=&rdquo;&rdquo;.</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • robotWithString

      public String robotWithString(String s)