-
Notifications
You must be signed in to change notification settings - Fork 613
/
1081.py
51 lines (40 loc) · 1.05 KB
/
1081.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
40
41
42
43
44
45
46
47
48
49
50
51
'''
Return the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once.
Example 1:
Input: "cdadabcc"
Output: "adbc"
Example 2:
Input: "abcd"
Output: "abcd"
Example 3:
Input: "ecbacba"
Output: "eacb"
Example 4:
Input: "leetcode"
Output: "letcod"
Note:
1 <= text.length <= 1000
text consists of lowercase English letters.
'''
class Solution(object):
def smallestSubsequence(self, text):
"""
:type text: str
:rtype: str
"""
if not text:
return ''
import collections
freq_map = collections.Counter(text)
used = [False]*26
result = ''
for char in text:
freq_map[char] -= 1
if used[ord(char)-97]:
continue
while (result and result[-1] > char and freq_map[result[-1]] > 0):
used[ord(result[-1])-97] = False
result = result[:-1]
used[ord(char)-97] = True
result += char
return result