leetcode-Minimum Window Substring
2015-11-03 21:09
429 查看
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 =
T =
Minimum window is
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.
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.
class Solution { public: string minWindow(string s, string t) { int slen=s.length(); int tlen=t.length(); if(tlen<=0||tlen>slen) return string(""); map<char,int> S; map<char,int> T; for(auto &e:t) ++T[e]; string res; int end=0,begin=0,minlen=INT_MAX; int count=0; for(;end<slen;++end){ if(T.find(s[end])==T.end()) continue; ++S[s[end]]; if(S[s[end]]<=T[s[end]]) ++count; if(tlen==count){ while(T.find(s[begin])==T.end()||S[s[begin]]>T[s[begin]]){ if(T.find(s[begin])!=T.end()) --S[s[begin]]; ++begin; } if(end-begin+1<minlen){ minlen=end-begin+1; res=s.substr(begin,minlen); } } } return res; } };
相关文章推荐
- 使用系统类NSURLSessionDownloadTask实现断点续传(音乐视频下载等)
- C/C++基本数据类型大小及表示范围
- SUMO使用教程(五)
- .NET中值得体验的精妙设计
- LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)
- MagicalRecord的使用(入门级别)
- CUDA-GDB调试一般方法————矩阵乘的例子
- windows下安装lavavel5
- nyoj57--6174问题
- EF在转换成JsonResult时遇到无限循环的解决办法
- IOS关闭键盘以及接受键盘打开关闭事件
- java中求一个字符创的最大不重复长度
- git学习三:git常用命令
- 判断节点为元素节点 HTML XML
- SUMO使用教程(四)
- Memcached 缓存个体,对象,泛型,表
- 【UKIEPC2015 H】【凸包思维栈操作】Sunlight 每座建筑的日照时间
- JS 制作九九乘法表
- Mapreduce与Hive比较
- [NOIP 2012]国王游戏 高精度+贪心