#3039

Apply Operations to Make String Empty

specialist · 760 · lc medium +31 · verified · 57.4% accepted · 167 likes · top 53%

Description

You are given a string s.

Repeatedly perform the following until s is empty: scan 'a' to 'z' and remove the first occurrence of each letter that exists in s.

Return the value of s just before the final operation is applied.

For example, starting with s = "aabcbbca": after the first pass s = "abbca", after the second s = "ba", after the third s = "". The answer is "ba".

Example 1:

Input: s = "aabcbbca"
Output: "ba"
Explanation: Explained in the statement.

Example 2:

Input: s = "abcd"
Output: "abcd"
Explanation: We do the following operation:
- Remove the underlined characters s = "abcd". The resulting string is s = "".
The string just before the last operation is "abcd".

Code

1
2
3