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 there is no such substring, return the empty string "".
The testcases will be generated such that the answer is unique.
문자열 s와 t가 주어집니다.
문자열 t에 있는 모든 문자를 포함하는 가장 작은 s의 부분문자열을 구해 리턴하세요
답은 반드시 한개 이하가 존재하며, 답이 없을시 빈 문자열을 리턴하세요
대소문자를 구분합니다.
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: BANC는 ABC를 전부 포함하면서 가장 작은 부분문자열이다.
characters = dict()
for ch in t:
if ch in characters:
characters[ch] += 1
else:
characters[ch] = 1
# t 가 'ABC' 일시, {'A': 1, 'B': 1, 'C': 1} 이 된다.
for right, ch in enumerate(s):
if ch in characters:
characters[ch] -= 1
if characters[ch] >= 0:
count += 1
if count == length:
have = True
while left <= right:
if s[left] in characters:
characters[s[left]] += 1
if characters[s[left]] > 0:
count -= 1
ans = ans if len(ans) < (right-left+1) else s[left:right+1]
left += 1
break
left += 1
class Solution:
def minWindow(self, s: str, t: str) -> str:
if len(s) < len(t): return ""
characters = dict()
for ch in t:
if ch in characters:
characters[ch] += 1
else:
characters[ch] = 1
length = len(t)
count = 0
left = 0
ans = s
have = False
for right, ch in enumerate(s):
if ch in characters:
characters[ch] -= 1
if characters[ch] >= 0:
count += 1
if count == length:
have = True
while left <= right:
if s[left] in characters:
characters[s[left]] += 1
if characters[s[left]] > 0:
count -= 1
ans = ans if len(ans) < (right-left+1) else s[left:right+1]
left += 1
break
left += 1
if not have: return ""
return ans