题解 | #最小覆盖子串#
最小覆盖子串
https://www.nowcoder.com/practice/c466d480d20c4c7c9d322d12ca7955ac
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 ''