Problem · String
Lexicographically Minimum Stack Encryption
Learn this problemProblem statement
You are given a string originalString. Start with two empty strings: temporaryString and encryptedString.
Repeat either of the following operations until both originalString and temporaryString are empty:
- Remove the first character of
originalStringand append it totemporaryString. - Remove the last character of
temporaryStringand append it toencryptedString.
Choose the operation order that produces the lexicographically smallest possible encryptedString, and return that string.
Function
minimumEncryptedString(originalString: String) → StringExamples
Example 1
originalString = "dby"return = "bdy"Move d and then b into the temporary stack. Pop b first, then arrange the remaining legal moves to produce bdy.
Example 2
originalString = "vgxgpu"return = "ggpuxv"The minimum legal encrypted string is ggpuxv.
Constraints
1 <= originalString.length() <= 200000originalStringcontains lowercase English letters.