-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathSolution.py
39 lines (26 loc) · 983 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
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.
Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
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.
"""
class Solution:
def removeDuplicateLetters(self, s: str) -> str:
last_occ = {c: i for i, c in enumerate(s)}
stack = ["!"]
visited = set()
for i, symbol in enumerate(s):
if symbol in visited:
continue
while (symbol < stack[-1] and last_occ[stack[-1]] > i):
visited.remove(stack.pop())
stack.append(symbol)
visited.add(symbol)
return "".join(stack)[1:]