Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 출처 문어박사
- dockerinstall
- springboot_exception_handler
- 스프링부트팔로잉
- 출처 따배도
- 스프링부트서버에사진전송
- 스프링사진업로드
- 인스타클론
- 스프링부트중복예외처리
- 스프링사진
- centos도커설치
- 출처 노마드코더
- ssh도커설치
- 우분투도커설치
- 스프링부트
- 출처 메타코딩
- WAS웹서버
- 스프링익셉션처리
- 스프링부트api
- 파이썬sort
- 스프링부트구독취소
- 스프링부트팔로우취소
- 멀티폼
- vm도커설치하는법
- 서버에도커설치
- 스프링이미지업로드
- 스프링구독
- 스프링부트사진올리기
- 출처 코딩셰프
- 도커설치하는법
Archives
- Today
- Total
MakerHyeon
[LeetCode] 76. Minimum Window Substring 본문
https://leetcode.com/problems/minimum-window-substring/
Minimum Window Substring - LeetCode
Can you solve this real interview question? Minimum Window Substring - Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If t
leetcode.com
t의 문자들이 속해있는 가장 최소 문자열을 찾는 문제이다.
- t 안의 각 문자 당 문자 개수를 센다. (count 초기화)
- 한 개 문자의 count 조건을 만족하면(window[c] == countT[c]) have 증가
- have == need 라면
- 최소길이 결과 업데이트
- 맨 왼쪽문자 pop (문자길이 줄여가기 위함)
- count조건 만족x라면 have 감소
- L pointer 증가
SOLUTION CODE
# PYTHON
class Solution:
def minWindow(self, s: str, t: str) -> str:
if t == "":
return ""
countT, window = {}, {}
# countT initialize
for c in t:
countT[c] = 1 + countT.get(c, 0)
have, need = 0, len(countT)
res, resLen = [-1, -1], float("infinity")
l = 0
for r in range(len(s)):
c = s[r] # 각 문자에 대해
window[c] = 1 + window.get(c, 0) # count 증가
# t안의 문자에 대해서만 비교 + count조건만족o
if c in countT and window[c] == countT[c]:
have += 1 # have 증가
# 만약 같다면(조건 만족)
while have == need:
# 결과 업데이트
if (r - l + 1) < resLen:
res = [l, r]
resLen = r - l + 1
# pop from the left of our window
window[s[l]] -= 1
# t안의 문자 + count조건 만족X
if s[l] in countT and window[s[l]] < countT[s[l]]:
have -= 1
l += 1
l, r = res
return s[l : r + 1] if resLen != float("infinity") else ""
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode] 567. Permutation in String (0) | 2023.04.28 |
---|---|
[LeetCode] 424. Longest Repeating Character Replacement (0) | 2023.04.27 |
[LeetCode] 3. Longest Substring Without Repeating Characters (0) | 2023.04.25 |
[LeetCode] 121. Best Time to Buy and Sell Stock (0) | 2023.04.19 |
[LeetCode] 42. Trapping Rain Water (python) (0) | 2023.04.10 |
Comments