1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| from collections import defaultdict class Solution: def minWindow(self, s: str, t: str) -> str: left = 0 right = 0 res = "" need = defaultdict(int) window = defaultdict(int) minLen = len(s)
for i in t: need[i] += 1
while right < len(s): window[s[right]] += 1 while self.con(need,window): if len(s[left:right+1]) < minLen: minLen = len(s[left:right+1]) res = s[left:right+1] window[s[left]] -= 1 left += 1 right += 1
return res
def con(self, need, window): for key in need.keys(): if need[key] > window[key]: return False return True
|