3170. Lexicographically Minimum String After Removing Stars
You are given a string
s. It may contain any number of'*'characters. Your task is to remove all'*'characters.While there is a
'*', do the following operation:
- Delete the leftmost
'*'and the smallest non-'*'character to its left. If there are several smallest characters, you can delete any of them.Return the lexicographically smallest resulting string after removing all
'*'characters.Example 1:
Input: s = "aaba*"
Output: "aab"
Explanation:
We should delete one of the
'a'characters with'*'. If we chooses[3],sbecomes the lexicographically smallest.Example 2:
Input: s = "abc"
Output: "abc"
Explanation:
There is no
'*'in the string.
def clearStars(self, s: str) -> str:
cnt = [[] for _ in range(26)]
arr = list(s)
for i, c in enumerate(arr):
if c != "*":
cnt[ord(c) - ord("a")].append(i)
else:
for j in range(26):
if cnt[j]:
arr[cnt[j].pop()] = "*"
break
return "".join(c for c in arr if c != "*")