박상길, 『파이썬 알고리즘 인터뷰』를 정리한 내용입니다.
https://leetcode.com/problems/remove-duplicate-letters
Remove Duplicate Letters - LeetCode
Can you solve this real interview question? Remove Duplicate Letters - Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible re
leetcode.com
class Solution:
def removeDuplicateLetters(self, s: str) -> str:
counter, stack = collections.Counter(s), []
for char in s:
counter[char] -= 1
if char in stack:
continue
while stack and char < stack[-1] and counter[stack[-1]] > 0:
stack.pop()
stack.append(char)
return ''.join(stack)
[LeetCode] Combination Sum | DFS | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (2) | 2023.06.22 |
---|---|
[LeetCode] Longest Substring Without Repeating Characters | 투포인터 | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (1) | 2023.06.19 |
[프로그래머스] 148653. 마법의 엘리베이터| deque | 파이썬, 소스코드, 정답 (0) | 2023.06.14 |
[LeetCode] 3Sum | 투포인터 | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (1) | 2023.06.09 |
[프로그래머스] 161988. 연속 펄스 부분 수열의 합| DP | 파이썬, 소스코드, 정답 (1) | 2023.06.04 |