题目:
题解:
class Solution:
def removeDuplicateLetters(self, s: str) -> str:
vis = defaultdict(int)
cnt = defaultdict(int)
for ch in s: cnt[ch] += 1
queue = []
for ch in s:
if vis[ch] == 0:
while queue and queue[-1] > ch and cnt[queue[-1]]:
vis[queue.pop()] = 0
if not queue or queue != ch: queue.append(ch)
vis[ch] = 1
cnt[ch] -= 1
return "".join(queue)