You are given a string s and a robot that currently holds an empty string t. Apply one of the following operations until s and t are both empty:
-
Remove the first character of
s
and give it to the robot. The robot appends this character to
t
.
-
Remove the last character of
t
and give it to the robot. The robot writes this character on paper.
Return the lexicographically smallest string that can be written on paper.
Function Signature
def robot_with_string(s: str) -> str:
...
Rules
-
The paper is finished only when both
s
and
t
are empty, so the returned string contains every character of
s
exactly once.
-
t
only grows at its end and only shrinks from its end, so it behaves like a stack.
Constraints
-
1 <= len(s) <= 10^5
-
s
consists only of lowercase English letters.
Examples
Input: s = "zza"
Output: "azz"
The paper should start with a, the smallest letter. Since a is the last character of s, both z characters must already be in t when it is written, and they then come off the end of t one after the other.
Input: s = "bac"
Output: "abc"
Push b, push a, write a, write b, then push c and write it.
Input: s = "bdda"
Output: "addb"
Starting with a requires b, d and d to be in t first. After a is written, t holds "bdd" and empties from its end as d, d, b.