Minimum Window Substring
Hard
Subject: Sliding Window
Time Complexity
O(M + N)
Space Complexity
O(M + N)
Problem Description
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.
Optimal Solution
Pythonfrom collections import Counter
def minWindow(s, t):
if not s or not t:
return ""
dict_t = Counter(t)
required = len(dict_t)
left, right = 0, 0
formed = 0
window_counts = {}
ans = float("inf"), None, None
while right < len(s):
char = s[right]
window_counts[char] = window_counts.get(char, 0) + 1
if char in dict_t and window_counts[char] == dict_t[char]:
formed += 1
while left <= right and formed == required:
if right - left + 1 < ans[0]:
ans = (right - left + 1, left, right)
char = s[left]
window_counts[char] -= 1
if char in dict_t and window_counts[char] < dict_t[char]:
formed -= 1
left += 1
right += 1
return "" if ans[0] == float("inf") else s[ans[1]:ans[2] + 1]