You are given a string s made of lowercase English letters and any number of '*' characters. Your task is to remove all '*' characters.
While there is a '*' in s, do the following operation:
-
Delete the leftmost
'*'
and the smallest non-
'*'
character to its left. If there are several copies of that smallest character, you may delete any one of them.
Different choices among tied copies can leave different strings. Return the lexicographically smallest string that can remain after all stars have been removed. The input is generated so that the operation is always possible.
Function Signature
def clear_stars(s: str) -> str:
...
Rules
-
"Smallest" means earliest in the alphabet. Only characters still present in
s
at the moment the star is processed count.
-
Each star removes exactly one letter, so every reachable result has the same length and the lexicographically smallest one is unique.
-
The result may be the empty string.
Constraints
-
1 <= len(s) <= 10^5
-
s
consists of lowercase English letters and
'*'
.
-
The input is generated such that it is possible to delete all
'*'
characters.
Examples
Input: s = "aaba*"
Output: "aab"
The star must delete an 'a', and there are copies at indices 0, 1 and 3. Deleting index 0 or 1 leaves "aba", while deleting index 3 leaves "aab", which is smaller.
Input: s = "abc*de*f"
Output: "cdef"
The star at index 3 sees a, b, c and deletes the 'a'. The star at index 6 then sees b, c, d, e and deletes the 'b', which leaves "cdef".
Input: s = "de*a*"
Output: "e"
The first star only sees d and e, so it deletes the 'd' even though an 'a' comes later. The second star then sees e and a and deletes the 'a'.