Question
To get the lexicographically smallest string that can be written on paper, I simulate the robot's operations using a monotonic greedy approach. As I traverse the input string s
, I push each character into a stack t
(representing the robot's hand). Before moving to the next character, I decide whether to pop from the stack and write to the result based on whether the top of the stack is less than or equal to the smallest character remaining in s
. To do this efficiently, I precompute an array min_char
where min_char[i]
stores the smallest character from s[i]
to the end. At each step, if the top of the stack is smaller than or equal to the future minimum, I pop it and add it to the result.