-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path316.py
More file actions
29 lines (21 loc) · 724 Bytes
/
316.py
File metadata and controls
29 lines (21 loc) · 724 Bytes
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
# def removeDuplicateLetters(s):
# for char in sorted(set(s)):
# suffix = s[s.index(char):]
# if set(s) == set(suffix):
# return char + removeDuplicateLetters(suffix.replace(char, ''))
# return ''
# 스택을 이용해서 풀어보면
import collections
def removeDuplicateLetters(s):
counter, seen, stack = collections.Counter(s),set(),[]
for char in s:
counter[char] -= 1
if char in seen:
continue
while stack and char < stack[-1] and counter[stack[-1]] >0:
seen.remove(stack.pop())
stack.append(char)
seen.add(char)
return ''.join(stack)
s = 'cbacdcbc'
print(removeDuplicateLetters(s))