class Solution: def minWindow(self , S: str, T: str) -> str: if not T or not S: return '' dic = {} for c in T: dic[c] = dic.get(c, 0) + 1 res = [] for i in range(len(S)): count = 0 record = dic.copy() if S[i] in record: start = i record[S[i]] -= 1 count += 1 if count == len(T): return S[i] for j in range(i + 1, len(S)): if S[j] in record: if record[S[j]] == 0: continue elif record[S[j]] > 0: record[S[j]] -= 1 count += 1 if count == len(T): tmp = S[start:j + 1] res.append([tmp, len(tmp)]) res = sorted(res, key=lambda x: x[1]) return res[0][0] if res else ''