Remove duplicate letters
class Solution:
def removeDuplicateLetters(self, s: str) -> str:
stk = []
count = [0] * 256
for i in range(len(s)):
count[ord(s[i])] += 1
in_stack = [False] * 256
for c in s:
count[ord(c)] -= 1
if in_stack[ord(c)]:
continue
while stk and stk[-1] > c:
if count[ord(stk[-1])] == 0:
break
in_stack[ord(stk.pop())] = False
stk.append(c)
in_stack[ord(c)] = True
return "".join(stk)
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 results.
Example 1:
Input: s = "bcabc" Output: "abc"
Example 2:
Input: s = "cbacdcbc" Output: "acdb"
Constraints:
1 <= s.length <= 104sconsists of lowercase English letters.
Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/