[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 in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
问题: 给定字符串 S 和 T ,求 S 中包含所有 T 中字符的最小子字符串。
值得一提的是,“包含所有 T 中的字符”,是要包含 T 中所有字符以及对应的个数。在 T 中重复出现的字符,在最后答案中也需要包含相同个数的重复字符。第一次解没有理解到这个点,提出结果错误,意识到这个点后,修正并提交成功。
解题思路采用的也是 滑动窗口算法(Slide Window Algorithm),LeetCode 把这样的算法归类到 双指针(Two Pointers)类型,算法思路和前面的 Longest Substring Without Repeating Characters 以及 Minimum Size Subarray Sum 相似。
设下标 l 和 r,把左开右闭 [l, r) 想象成一个窗口。
- 当窗口包含所有 T 中的字符时,则此时的窗口是一个可行解,l++
- 当窗口没有包含所有 T 中的字符时,则 r++;
如何判断窗口是否包含所有 T 中的字符呢?我使用了 map<char, alphStatus> 来表示,其中 alphStatus 只包含两个值:对应的字符在 T 中出现次数(target),以及当前窗口包含该字符的个数(cur)。可见,当 cur > target 时,对应字符满足被包含条件。
当所有字符都满足被包含条件时,当前窗口便是一个可行解。
找出所有可行解中窗口最小的,便是原题目的解。
class alphStatus{
public:
int target = ;
int cur = ;
};
string minWindow(string s, string t) {
if (s.size() == ) {
return (s == t) ? s : "";
}
int l = ;
int r = ;
map<char, alphStatus> alph_status;
for (int i = ; i < t.size(); i++) {
alph_status[t[i]].target++;
}
if (alph_status.count(s[]) != ) {
alph_status[s[]].cur = ;
}
string res = s + "$";
while (r <= s.size()) {
bool isAllConted = true;
map<char, alphStatus>::iterator m_iter;
for (m_iter = alph_status.begin(); m_iter != alph_status.end(); m_iter++) {
if (m_iter->second.cur < m_iter->second.target) {
isAllConted = false;
break;
}
}
if ( isAllConted ) {
if (r-l < res.size()) {
res = s.substr(l, r-l);
}
if (alph_status.count(s[l]) != ) {
alph_status[s[l]].cur--;
}
l++;
}else{
if (r < s.size() && alph_status.count(s[r]) != ) {
alph_status[s[r]].cur++;
}
r++;
}
}
return ((int)res.size() == (int)s.size()+) ? "" : res;
}
[LeetCode] 76. Minimum Window Substring 解题思路的更多相关文章
- [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】Minimum Window Substring (hard) ★
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 76. Minimum Window Substring (String, Map)
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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...
随机推荐
- AJAX 一些常用方法
abort() 停止当前请求getAllResponseHeaders() 返回包含HTTP请求的所有响应头信息,其中响应头包括Content-Length,Date,URI等内容.getRespon ...
- .net开发人员等级
.net 开发人员的瓶颈和职业发展 现在社会比前几年浮躁了,越来越多的人抱怨薪水低,高薪工作不好找; 诚然这有CPI的压力,可是也有很多人没有认清自己的职业发展. 很多.net程序员个各种纠结,想拿高 ...
- 跨域技术-jsonp
JSONP是JSON with padding 的简写,其有两个部分组成,一个是回调函数,一个是数据,其基本格式如下 function handleResult(result){ alert(resu ...
- 在Apache下开启SSI配置支持include shtml html和快速配置服务器
作为前端开发,使用Apache快速搭建服务器极为方便. 1.找到apach安装目录,找到conf目录下 的httpd.conf 使用SSI(Server Side Include)的html文件扩展名 ...
- Python全栈开发之MySQL(二)------navicate和python操作MySQL
一:Navicate的安装 1.什么是navicate? Navicat是一套快速.可靠并价格相宜的数据库管理工具,专为简化数据库的管理及降低系统管理成本而设.它的设计符合数据库管理员.开发人员及中小 ...
- js 小数格式化函数
直接上代码,参数number为待格式化整数或小数,fix是要保留有效位数,过亿以亿结尾,过万以万结尾,toFixed函数记得,免得再查 function shorten_number (number, ...
- Entity Framework Code First 映射继承关系
转载 http://www.th7.cn/Program/net/201301/122153.shtml Code First如何处理类之间的继承关系.Entity Framework Code Fi ...
- android SurfaceView绘制 重新学习--基础绘制
自从大二写了个android游戏去参加比赛,之后就一直写应用,一直没用过SurfaceView了,现在进入了游戏公司,准备从基础开始重新快速的学一下这个,然后再去研究openGL和游戏引擎. 直接上代 ...
- Xcode8和iOS10的适配问题
本文转自:http://www.jianshu.com/p/90d5323cf510 =================== 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功 ...
- JQuery replace 替换全部
天在做写个程序时遇到需要替换的功能,可是一开始用jquery的replace时,发现只替换到第一个.最后没办法,只好用正则表达式来例如下面 re = new RegExp("{thisc ...