일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 링피트
- Writing
- 파비최
- realclass
- 프로젝트
- 10분
- 월간
- 뭐든
- 리얼 클래스
- 3줄정리
- leetcode
- 미드시청
- 화상영어
- English
- Daily Challenge
- 사이드
- 개발자
- 잡생각
- 읽기
- 영어원서읽기
- FIT XR
- Problem Solving
- 영어공부
- 괜찮음
- 만화도
- 운동
- 30분
- 쓰릴오브파이트
- 매일
- 스탭퍼
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.09.26 Today's Challenge 본문
https://leetcode.com/problems/remove-duplicate-letters/
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
스택을 이용한 문제. 처음부터 감이 오긴 했는데..
세부 조건에 대한 로직 추가가 필요하다.
class Solution:
def removeDuplicateLetters(self, s: str) -> str:
last_occ = {}
st = []
visited = set()
for i in range(len(s)):
last_occ[s[i]] = i
for i in range(len(s)):
if s[i] not in visited:
while (st and st[-1] > s[i] and last_occ[st[-1]] > i):
visited.remove(st.pop())
st.append(s[i])
visited.add(s[i])
return ''.join(st)
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.09.28 Today's Challenge (0) | 2023.09.28 |
---|---|
2023.09.27 Today's Challenge (0) | 2023.09.27 |
2023.09.25 Today's Challenge (0) | 2023.09.25 |
2023.09.24 Today's Challenge (0) | 2023.09.24 |
2023.09.23 Today's Challenge (0) | 2023.09.23 |
Comments