作者: 负雪明烛
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. shell 小数的比较大小问题

    经过实验,if 语句中的数值判断是不可以比较小数大小的:-gt -ne 2. 直接用awk awk -v  num1=6.6 -v num2=5.5  'BEGIN{print(num1>num ...

  2. phpMyAdmin简介及安装

    phpMyAdmin是一个MySQL数据库管理工具,通过Web接口管理数据库方便快捷. Linux系统安装phpMyAdmin phpMyAdmin是一个MySQL数据库管理工具,通过Web接口管理数 ...

  3. 【玩具】获取B站视频的音频片段

    事情是这样的,我有个和社畜的社会地位不太相符的小爱好--听音乐剧. 基本上是在B站上点开视频听,不是不想在网易云或者QQ音乐听,只是在这些音乐软件上面,我想听的片段要不就收费,要不版本不是我喜欢的,要 ...

  4. C# js获取buttonid

    var id= document.getElementById('<%=控件的ID.ClientID %>');

  5. == 和 equals() 方法的区别

    == 在比较基本数据类型时,是比较两边的数据的值是否相等 // 整数类型 int num1 = 1; // 双精度浮点数类型 double num2 = 1.0; // 输出结果为 true Syst ...

  6. 前端4 — jQuery — 更新完毕

    1.下载jQuery 网址:Download jQuery | jQuery  最好下载最新版的,因为有什么bug问题,最新版的都会有,所以学技术就用最新版的,实际开发用的时候就要讲究了 2.开始用j ...

  7. 日常Java 2021/10/6

    声明自定义异常 class zidingyiException extends Exception{}//定义自己的异常类 单继承 public class A  {} public class B ...

  8. 零基础学习java------day15--------collections用法,比较器,Set(TreeSet,TreeMap),异常

    1. Collections用法 Collections: 集合的工具类public static <T> void sort(List<T> list) 排序,升序publi ...

  9. RHEL 6.5安装系统

    转自如下链接: http://www.jb51.net/os/128752.html

  10. 微服务中心Eureka

    一.简介 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS(AWS 是业务流程管理开发平台AWS Enterprise BPM Platform ...