作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/minimum-window-substring/description/

题目描述

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

Example:

Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"

Note:

  • If there is no such window in S that covers all characters in T, return the empty string “”.
  • If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

题目大意

在S中找出包含T的全部字符的最小子串。

解题方法

滑动窗口

统计字符出现的个数,而且时间复杂度要求 O(N),明显使用滑动窗口解题。和567. Permutation in String有点相似,都是要求子字符串满足一定的次数要求。

什么场景下使用滑动窗口? 答:如果我们找到了一个满足要求的区间,并且当区间的右边界再向右扩张已没有意义,此时可以移动左边界到达不满足要求的位置。再移动右边界,持续如此,直到区间的右边界到达整体的结束点。

比如本题:当我们在 s 中找到了一个覆盖了 t 的所有字符的子字符串 s[left, right] 时,如果再向右移动 right ,扩大范围后的新的子字符串仍然覆盖了 t 的所有字符,但新子字符串一定不是最短了(题目要求最短的子字符串)。这就是我说的右边界向右扩张已没有意义,此时应该移动左边界直至使得区间不满足子字符串不满足覆盖 t 。

本题做法:

  1. 定义 left, right 分别指向滑动窗口的左右边界,子字符串为 s[left, right] 双闭区间。

  2. 使用 right 指针向右搜索,同时要记录在 s[left, right] 这个区间覆盖了多少个 t 中的元素。如果在 s[left,right] 内,覆盖了所有 t 的元素,说明这个区间是符合要求的一个区间。此时 right指针再向右移动已经没有意义。

  3. 现在要移动 left 指针,直至 s[left,right] 子字符串不能覆盖 t

统计字符出现的次数可以直接使用字典,但如果对于每个 s[left, right] 区间都去统计一遍所有元素出现的次数,会导致方法复杂度会增加到 O(N ^ 2),因此不能通过 OJ。必须快速地判定 s[left,right] 是否覆盖了 t

题目说,在 s[left, right] 闭区间内的元素出现个数应该把 t 中所有的元素都包含,所以我们定义 scount 字典变量保存s[left, right] 闭区间中各个元素出现的次数;定义 tcount 字典变量保存 t 中的元素出现次数。 cnt 变量储存 s[left, right] 闭区间中已经覆盖了 t 中的多少个元素,如果 cnt == t.size() 说明该子数组覆盖了 t

在移动 left 指针的时候要注意存储最短的子串,使用的 minLen 变量存储当前满足题目要求的最短子串长度。当某个子字符串区间满足要求时,根据minLen更新最终的最短子串结果 res

这个题难点就在于维护 cnt,它的作用仅仅为了提高速度。

时间复杂度是O(N),空间复杂度是O(N)

这个题的 C++ 代码如下:

class Solution {
public:
string minWindow(string s, string t) {
int M = s.size();
int N = t.size();
unordered_map<char, int> scount;
unordered_map<char, int> tcount;
// left and right means [left, right] of s
// count means the same chars of s[left, right] with t
int left = 0, right = 0, count = 0;
int minLen = INT_MAX;
string res;
for (char c : t)
++tcount[c];
while (right < M) {
char c = s[right];
scount[c] += 1;
if (tcount.count(c) && scount[c] <= tcount[c]) {
count += 1;
}
while (left <= right && count == N) {
if (minLen > right - left + 1) {
minLen = right - left + 1;
res = s.substr(left, minLen);
}
char l = s[left];
scount[l] -= 1;
if (tcount.count(l) && scount[l] < tcount[l])
count -= 1;
++left;
}
++right;
}
return res;
}
};

参考资料:

https://leetcode.com/articles/minimum-window-substring/
http://www.cnblogs.com/grandyang/p/4340948.html

日期

2018 年 10 月 3 日 —— 玩游戏导致没睡好,脑子是浆糊。
2020 年 5 月 23 日 —— 这次编辑时对滑动窗口有了更深的理解

【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)的更多相关文章

  1. [LeetCode] 76. Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  2. [LeetCode] 76. Minimum Window Substring 解题思路

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  3. [leetcode]76. Minimum Window Substring最小字符串窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  4. Leetcode#76 Minimum Window Substring

    原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...

  5. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

  6. 【LeetCode】76. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  7. 【一天一道LeetCode】#76. Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. [Leetcode][JAVA] Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  9. lintcode 中等题:minimum window substring 最小子串覆盖

    题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

随机推荐

  1. window修改dns本地文件

    文件地址: C:\Windows\System32\drivers\etc 先修改权限: 最后用记事本打开编辑保存即可

  2. 压力测试工具——apchebench(简称ab)

    ab的原理 ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx.li ...

  3. Excel-在整个工作簿中查找/替换

    13.在整个工作簿中查找/替换 调范围为:工作簿,默认是工作表:

  4. EXCEL-表格安全性:加密给与不同操作权限、表格怎么不让别人复制粘贴?

    1.下方表格名称右键-保护工作表,将所有选项取消勾选 然后设置一个密码就可以了 2.文件旁的三条线点开-选项-安全性,设置编辑权限密码,确定

  5. cvc-complex-type.2.3: Element 'servlet' cannot have character [children], because the type's content

    错误原因:粘贴代码 <servlet> <servlet-name>barServlet</servlet-name> <servlet-class>S ...

  6. C语言大小端判定

    要判定大小端?需要弄清以下几个问题: 1.当一个变量占多个字节时,变量的指针指向的是低地址 2.什么是大小端? 大端模式:是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中. 小 ...

  7. 编程之美Q1

    题目 和数书页有点类似,就直接数吧 #include<iostream> using namespace std; class q1 { public: size_t func(size_ ...

  8. 【swift】长按事件绑定,平移滑动事件+坐标获取

    为何把这两个事件归类在一起? 我后来才明白,iOS有一个手势事件(UiGestureRecognizer) 事件里有7个功能,不过我只试过前两个,也就是标题的这两个(长按.平移滑动) UILongPr ...

  9. recyclerView DiffUtil使用

    DiffUtil是和RecyclerView一块用的,DiffUtil用来比较两个数据集,他的最大用处是在RecyclerView刷新时,不在无脑. 以前adapter.notifyDataSetCh ...

  10. redis入门到精通系列(二):redis操作的两个实践案例

    在前面一篇博客中我们已经学完了redis的五种数据类型操作,回顾一下,五种操作类型分别为:字符串类型(string).列表类型(list).散列类型(hash).集合类型(set).有序集合类型(so ...