【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
作者: 负雪明烛
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 。
本题做法:
定义
left,right分别指向滑动窗口的左右边界,子字符串为s[left, right]双闭区间。使用
right指针向右搜索,同时要记录在s[left, right]这个区间覆盖了多少个t中的元素。如果在s[left,right]内,覆盖了所有 t 的元素,说明这个区间是符合要求的一个区间。此时right指针再向右移动已经没有意义。现在要移动
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++)的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- Leetcode#76 Minimum Window Substring
原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 【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 ...
- 【一天一道LeetCode】#76. Minimum Window Substring
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 ...
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
随机推荐
- Perl哈希%hash
哈希是 key/value 键/值对的集合. Perl中哈希变量以百分号 (%) 标记开始. 访问哈希元素格式:${key}. 以下是一个简单的哈希实例: 实例 #!/usr/bin/perl %da ...
- typedef定义数组
typedef定义数组 问题来源 在学习高一凡数据结构与算法解析串这一章节时,遇到如下代码不明白其意义,经过查阅终于搞明白 typedef unsigned char SString[MAXLEN + ...
- 淘宝、网易移动端 px 转换 rem 原理,Vue-cli 实现 px 转换 rem
在过去的一段时间里面一直在使用Vue配合 lib-flexible和px2rem-loader配合做移动端的网页适配.秉着求知的思想,今天决定对他的原理进行分析.目前网上比较主流使用的就是淘宝方 ...
- linux添加用户、权限
# useradd –d /usr/sam -m sam 此命令创建了一个用户sam,其中-d和-m选项用来为登录名sam产生一个主目录/usr/sam(/usr为默认的用户主目录所在的父目录). 假 ...
- Output of C++ Program | Set 17
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- android 跳到应用市场给软件评分
1 String packetName = this.getPackageName(); 2 Uri uri = Uri.parse("market://details?id=" ...
- 深入 char
深入 char * ,char ** ,char a[ ] ,char *a[] 内核分类: c语言 2013-02-23 15:34 15176人阅读 评论(8) 收藏 举报Charcharchar ...
- BS版本的TCP程序
// 使用Socket对象中的方法getInputStream,获取到网络字节输入流InputStream对象 InputStream is = socket.getInputStream();// ...
- 使用MyBatis框架时发现的一些小bug
在大配置MyBatis.xml中: 不能有空节点属性 ,否则启动服务器后点击登录没有反应. 异常问题: ause: java.sql.SQLException: Value '0000-00-00 ...
- CTF靶场
CTF靶场测试报告 一.跨站脚本攻击(XSS) 实验原理:跨站脚本攻击( Cross Site Script),本来的缩写应为CSS,但是为了与层叠样式表(Cascading Style CSS)区分 ...