classSolution(object): defminWindow(self, s, t): """ :type s: str :type t: str :rtype: str """ ifnot s: return'' expected = defaultdict(int) appeared = defaultdict(int) for c in t: expected[c] += 1
i = j = 0 m = len(t) n = len(s) min_window = None while i < n and j < n: while j < n and m > 0: if appeared[s[j]] < expected[s[j]]: m -= 1 appeared[s[j]] += 1 j += 1
if m == 0: # found a window, minimize it while i < n and appeared[s[i]] > expected[s[i]]: appeared[s[i]] -= 1 i += 1 ifnot min_window or (j - i) < (min_window[1] - min_window[0]): min_window = (i, j) # i++ appeared[s[i]] -= 1 m += 1 i += 1 return min_window and s[min_window[0]: min_window[1]] or''