https://leetcode.com/problems/remove-duplicate-letters/
class Solution:
def removeDuplicateLetters(self, s: str) -> str:
last_occ = {}
visited = set()
stack = []
for i in range(len(s)):
last_occ[s[i]] = i
for i in range(len(s)):
if s[i] not in visited:
while (stack and stack[-1] > s[i] and last_occ[stack[-1]] > i):
visited.remove(stack.pop())
stack.append(s[i])
visited.add(s[i])
return ''.join(stack)
'Programming > LeetCode' 카테고리의 다른 글
[LeetCode] Implement Stack using Queues(큐로 스택 구현) (0) | 2022.03.29 |
---|---|
[LeetCode] Daily Temperatures (0) | 2022.03.29 |
[LeetCode] Number of Islands (0) | 2022.03.25 |
[LeetCode] Two City Scheduling (0) | 2022.03.25 |
[LeetCode] Valid Parentheses (0) | 2022.03.25 |
댓글