Problem · String

Lexicographically Minimum Stack Encryption

Learn this problem
MediumRipplingOA

Problem 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 originalString and append it to temporaryString.
  • Remove the last character of temporaryString and append it to encryptedString.

Choose the operation order that produces the lexicographically smallest possible encryptedString, and return that string.

Function

minimumEncryptedString(originalString: String) → String

Examples

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() <= 200000
  • originalString contains lowercase English letters.

More Rippling problems

drafts saved locally
public String minimumEncryptedString(String originalString) {
  // write your code here
}
originalString"dby"
expected"bdy"
checking account