Minimum Window Substring
Solution Code
class Solution:def minWindow(self, s: str, t: str) -> str:if not t or not s: return ""from collections import Countertarget_counts = Counter(t)required = len(target_counts)l, r = 0, 0formed = 0window_counts = {}ans = float("inf"), None, Nonewhile r < len(s):
char = s[r]
window_counts[char] = window_counts.get(char, 0) + 1
if char in target_counts and window_counts[char] == target_counts[char]:
formed += 1
while l <= r and formed == required:
char = s[l]
if r - l + 1 < ans[0]:
ans = (r - l + 1, l, r)
window_counts[char] -= 1
if char in target_counts and window_counts[char] < target_counts[char]:
formed -= 1
l += 1
r += 1
return "" if ans[0] == float("inf") else s[ans[1] : ans[2] + 1]Time Complexity
O(M + N) where M is the length of s and N is the length of t.
Space Complexity
O(K) where K is the number of unique characters in t (at most O(1) if the character set size is fixed).

