Skip to content

2434. Using a Robot to Print the Lexicographically Smallest String #1774

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the lexicographically smallest string that can be written on the paper by a robot using two operations: moving the first character of the string s to the robot's string t (stack), or moving the last character of t to the paper. The challenge is to determine the optimal sequence of operations that results in the smallest possible string.

Approach

  1. Problem Analysis: The robot can either push the first character of s onto a stack (t) or pop the top character from the stack to the result string (p). The goal is to arrange these operations such that the resulting string p is lexicographically smallest.
  2. Key Insight: The lexicographically smallest string can be achieved by output…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Jun 6, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jun 6, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants