#76
Minimum Window Substring
hard· Sliding Windowruns: 0Given two strings s and t, 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.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 938
class Solution:
def minWindow(self, s: str, t: str) -> str:
if not s or not t:
return ""
need = {}
for c in t:
need[c] = need.get(c, 0) + 1
required = len(need)
have = 0
window = {}
best = [-1, -1]
best_len = float("inf")
left = 0
for right in range(len(s)):
c = s[right]
window[c] = window.get(c, 0) + 1
if c in need and window[c] == need[c]:
have += 1
while have == required:
if (right - left + 1) < best_len:
best = [left, right]
best_len = right - left + 1
window[s[left]] -= 1
if s[left] in need and window[s[left]] < need[s[left]]:
have -= 1
left += 1
l, r = best
return s[l : r + 1] if best_len != float("inf") else ""
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.