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 解题思路的更多相关文章

  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

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

  4. 刷题76. Minimum Window Substring

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

  5. 【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 ...

  6. 【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 ...

  7. 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 ...

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

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

  9. 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...

随机推荐

  1. C#使用Json

    AJAX传递复杂数据如果自己进行格式定义的话会经历组装.解析的过程,因此AJAX中有一个事实上的数据传输标准JSon. Json将复杂对象序列化为一个字符串,在浏览器端再将字符串反序列化为JavaSc ...

  2. 疯狂学习java web

    因工作需要,疯狂学习java web,只是这么多年一直从事C++开发,突然之间要接手同事的那么一大堆代码,真有无从下手的感觉,首先是要学习html,然后是js, 然后是jsp,当然还有各种框架,想想就 ...

  3. CentOS6.5下docker的安装及遇到的问题和简单使用

    Docker是一个开源的应用容器引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.利用Linux的LXC.AUFS.Go语言.cgroup实现了资源的独立,可以很轻松的实现文件.资 ...

  4. nginx 中出现nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

    有其他的程序占用的80端口.只需把相关程序关闭,fuser -k 80/tcp 然后再次 /usr/local/nginx/sbin/nginx,就能开启nginx服务了

  5. nginx 安装过程中遇到的问题

    安装Nginx时报错 ./configure: error: the HTTP rewrite module requires the PCRE library. 安装pcre-devel解决问题yu ...

  6. REST接口规范

    参考文章 这篇文章使用不同的method代表不同操作 http://www.cnblogs.com/tommyli/p/3913018.html 实际应用中(我们过去的应用) 则是直接使用url来代表 ...

  7. jquery的return this.each()的作用

    经常看到在运用jquery插件绑定事件时候,都会用到each. 下面来比较下使用return this和return this.each()在使用的区别. 注意:使用each的时候引用this,必须使 ...

  8. which命令

    which指令会在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果. 格式 which 可执行文件名称 参数 -V  显示版本信息 实例 用 which 去找出 which w ...

  9. linux curl命令验证服务器断点续传支持

    有个同事说,发现现在对外下载安装包的服务器不支持断点续传,我听了一阵纳闷,lighttpd server对于静态文件应该默认支持断点续传的,登机器查看lighttpd配置文件发现 对断点续传的支持被禁 ...

  10. [转]CentOS Yum 命令详解

    总所周知,Redhat和Fedora的软件安装命令是rpm,但是用rpm安 装软件最大的麻烦就是需要手动寻找安装该软件所需要的一系列依赖关系,超级麻烦不说,要是软件不用了需要卸载的话由于卸载掉了某个依 ...