-
Notifications
You must be signed in to change notification settings - Fork 57
/
Solution.py
38 lines (26 loc) · 884 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
"""
Return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.
Note: This question is the same as 316: https://leetcode.com/problems/remove-duplicate-letters/
Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Constraints:
1 <= s.length <= 1000
s consists of lowercase English letters.
"""
class Solution:
def smallestSubsequence(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:]