알고리즘

Leetcode 316 - Remove Duplicate Letters (python)

Jaymyong66 2023. 4. 9. 19:37

Counter를 알게된 순간...

collections 에 어떤게 있는지 알아보는 계기가 되었다.

class Solution:
    def removeDuplicateLetters(self, s: str) -> str:

        counter, result, stack = collections.Counter(s), set(), []

        for c in s:
            counter[c] -= 1
            if c in result:
                continue

            while stack and c < stack[-1] and counter[stack[-1]] > 0:
                result.remove(stack.pop())

            stack.append(c)
            result.add(c)

        return ''.join(stack)

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

 

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

 

'알고리즘' 카테고리의 다른 글

백준 10845 - 큐 (class2)  (0) 2023.05.07
백준 10828 - 스택 (class2)  (0) 2023.05.07
Leetcode 1 - two sum (python)  (0) 2023.04.09
백준 9655 - 돌게임1 (python)  (0) 2023.04.09
백준 11650 - 좌표 정렬하기 (python)  (0) 2023.04.02