Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
remove-duplicate-letters.py 568 Bytes
Copy Edit Raw Blame History
Sanghee Kim authored 2019-02-03 20:27 +08:00 . update remove-duplicate-letter.py
# Time: O(n)
# Space: O(k), k is size of the alphabet
from collections import Counter
class Solution(object):
def removeDuplicateLetters(self, s):
"""
:type s: str
:rtype: str
"""
remaining = Counter(s)
in_stack, stk = set(), []
for c in s:
if c not in in_stack:
while stk and stk[-1] > c and remaining[stk[-1]]:
in_stack.remove(stk.pop())
stk += c
in_stack.add(c)
remaining[c] -= 1
return "".join(stk)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search