class Solution:
def removeDuplicateLetters(self, s: str) -> str:
counter, seen, stack = collections.Counter(s), set(), []
for char in s:
counter[char] -= 1
if char in seen:
continue
while stack and char < stack[-1] and counter[stack[-1]] > 0:
seen.remove(stack.pop())
stack.append(char)
seen.add(char)
return "".join(stack)
'자료구조와 알고리즘' 카테고리의 다른 글
225. Implement Stack using Queues(큐를 이용한 스택 구현) (0) | 2023.02.13 |
---|---|
739. Daily Temperatures(일일 온도) (0) | 2023.02.13 |
20. Valid Parentheses(유효한 괄호) (0) | 2023.02.13 |
2. Add Two Numbers(두 수의 덧셈) (0) | 2023.02.08 |
206. Reversed Linked List(역순 연결 리스트) (0) | 2023.02.08 |