1def min_window(s, t):
2 if not s or not t: return ''
3 dict_t = {}
4 for c in t: dict_t[c] = dict_t.get(c, 0) + 1
5 required = len(dict_t)
6 left = 0; formed = 0
7 window_counts = {}
8 ans = (float('inf'), None, None)
9 for right in range(len(s)):
10 char = s[right]
11 window_counts[char] = window_counts.get(char, 0) + 1
12 if char in dict_t and window_counts[char] == dict_t[char]:
13 formed += 1
14 while left <= right and formed == required:
15 if right - left + 1 < ans[0]:
16 ans = (right - left + 1, left, right)
17 char = s[left]
18 window_counts[char] -= 1
19 if char in dict_t and window_counts[char] < dict_t[char]:
20 formed -= 1
21 left += 1
22 return '' if ans[0] == float('inf') else s[ans[1]:ans[2]+1]