Apply Operations to Make String Empty

You are given a string s.

Consider performing the following operation until s becomes empty:

  • For every alphabet character from 'a' to 'z', remove the first occurrence of that character in s if it exists.

Return the value of the string s right before applying the last operation.

Example 1
Inputs = "aabcbbca"
Output"ba"
The operations eventually leave "ba" immediately before the final operation makes the string empty.
Example 2
Inputs = "abcd"
Output"abcd"
All characters are removed in the only operation, so the string just before the last operation is "abcd".

Constraints

  • 1 <= s.length <= 5 * 10^5
  • s consists only of lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate