AmazonOA #34
Get Minimum String (Box Lex-Smallest)
Estimated MediumJudge-ready
One operation takes out any digit d, changes it to min(d+1,9), and inserts it at any position. You may perform any number of operations (including 0). Find the lexicographically smallest final string, keeping leading zeros.
Input
One line containing a digit string of length 1..200000.
Output
Output the lexicographically smallest digit string, keeping leading zeros.
Example 1
Input:
26547
Output:
24677
Example 2
Input:
0
Output:
0
Example 3
Input:
21
Output:
13
Example notes
Example 1: 6 and 5 have a smaller 4 to their right, so they are moved and become 7 and 6; sorting them together with the kept 2, 4 and 7 gives 24677. Example 2: doing nothing is already the smallest, so the output is 0. Example 3: take 2 out, turning it into 3, and insert it after 1 to get 13.
Editorial
Loading solution…