316. Remove Duplicate Letters

Remove Duplicate Letters - LeetCode

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 results.

Example 1: Input: s = “bcabc” Output: “abc” Example 2: Input: s = “cbacdcbc” Output: “acdb”

Constraints:

1 <= s.length <= 104
s consists of lowercase English letters.

  • code
class Solution:
    def removeDuplicateLetters(self, s: str) -> str:
        stack = []
        seen = set()
        
        last_index = {c: i for i, c in enumerate(s)}
        
        for i, c in enumerate(s):
            if c not in seen:
                while stack and c < stack[-1] and i < last_index[stack[-1]]:
                    seen.discard(stack.pop())
                stack.append(c)
                seen.add(c)
        return "".join(stack)