-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path316_RemoveDuplicateLetters.py
More file actions
52 lines (41 loc) · 1.41 KB
/
316_RemoveDuplicateLetters.py
File metadata and controls
52 lines (41 loc) · 1.41 KB
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
52
# coding: utf8
"""
题目链接: https://leetcode.com/problems/remove-duplicate-letters/description.
题目描述:
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and
only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example:
Given "bcabc"
Return "abc"
Given "cbacdcbc"
Return "acdb"
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
"""
class Solution(object):
def removeDuplicateLetters(self, s):
"""
:type s: str
:rtype: str
"""
if not s or len(s) < 2:
return s
vis = [0] * 26
cnt = [0] * 26
ans = []
for c in s:
cnt[ord(c) - 97] += 1
# 遍历, 若当前字符已入栈, 跳过
# 否则, 与栈顶字符比较, 若小于栈顶字符且cnt[c] > 0(证明后面还会出现该字符), 出栈, 同时访问标记置为0
# 当前字符入栈, 访问标记置为1
for c in s:
idx = ord(c) - 97
cnt[idx] -= 1
if vis[idx]:
continue
while ans and c < ans[-1] and cnt[ord(ans[-1]) - 97]:
vis[ord(ans[-1]) - 97] = 0
ans.pop()
ans.append(c)
vis[idx] = 1
return ''.join(ans)