题目链接
题目描述
给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。
注意:
- 对于
t中重复字符,我们寻找的子字符串中该字符数量必须不少于t中该字符数量。 - 如果
s中存在这样的子串,我们保证它是唯一的答案。示例
示例1:
输入:s = “ADOBECODEBANC”, t = “ABC” 输出:“BANC”
提示
- 移动
end扩展窗口,直到包含t中所有字符。 - 移动
start缩小窗口,直到包含所有元素的临界点,即再移动就会不符合要求。 - 继续移动
start一次,重复步骤1。
使用哈希表辅助:
维护一个字典 count 来记录 t 中所有字符及其个数。
在遍历过程中,将相应的字符 count 值减一,若该字符不在 t 中,则 count 值会小于0。这样以来,count 值若为负数,则表示该字符是不需要的。
优化:
维护一个变量 totalLen 来记录当前状态所需要的字符的个数,当 totalLen == 0 时,表示当前窗口已经包含 t 所有元素,而不是去遍历字典。
题解
class Solution:def minWindow(self, s: str, t: str) -> str:count = collections.defaultdict(int)for c in t:count[c] += 1totalLen = len(t)n = len(s)start, end = 0, 0ans = (0, n)while end < n:if count[s[end]] > 0:totalLen -= 1count[s[end]] -= 1if totalLen == 0:while count[s[start]] < 0:count[s[start]] += 1start += 1ans = (start, end) if end - start < ans[1] - ans[0] else anscount[s[start]] += 1totalLen += 1start += 1end += 1return '' if ans[1] == n else s[ans[0]:ans[1] + 1]
复杂度分析
- 时间复杂度:
- 空间复杂度:
