본문 바로가기
python-algorithm

leetcode 316. Remove Duplicate Letters

by 무적김두칠 2023. 2. 23.

https://leetcode.com/problems/remove-duplicate-letters/description/

 

Remove Duplicate Letters - LeetCode

Can you solve this real interview question? Remove Duplicate Letters - Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible re

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def removeDuplicateLetters(self, s: str-> str:
        counter, duplicate_check, stack = collections.Counter(s), set(), []
 
        for char in s:
            counter[char]-=1
            if char in duplicate_check:
                continue
            while stack and char < stack[-1and counter[stack[-1]] > 0:
                duplicate_check.remove(stack.pop())
            stack.append(char)
            duplicate_check.add(char)
        
        answer = ''.join(stack)
 
        return answer
cs
반응형

'python-algorithm' 카테고리의 다른 글

leetcode 232. Implement Queue using Stacks  (0) 2023.02.23
leetcode 739. Daily Temperatures  (0) 2023.02.23
leetcode 20. Valid Parentheses  (0) 2023.02.23
백준 9773 ID Key  (0) 2023.02.21
leetcode 92. Reverse Linked List II  (0) 2023.02.17

댓글