Skip to content
LC-0316 Medium LeetCode

316. Remove Duplicate Letters

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 51% Topics: String, Stack, Greedy, Monotonic Stack
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(k), k is size of the alphabet

from collections import Counter


class Solution(object):
    def removeDuplicateLetters(self, s):
        """
        :type s: str
        :rtype: str
        """
        remaining = Counter(s)

        in_stack, stk = set(), []
        for c in s:
            if c not in in_stack:
                while stk and stk[-1] > c and remaining[stk[-1]]:
                    in_stack.remove(stk.pop())
                stk += c
                in_stack.add(c)
            remaining[c] -= 1
        return "".join(stk)

Solution from kamyu104/LeetCode-Solutions · MIT