Leetcode 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 emtpy 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的字符的查找时间,故要考虑用hashMap。hashMap的查找时间复杂度是O(1)。由于目标串T可能含有重复字符,因此使用一个哈希表记录目标串的字符和字符出现的次数,同时要记录搜索窗口的已匹配的字符数count,最小字符窗口的开始位置start,已匹配的字符和次数findHashMap.
当已匹配的总字符数大于目标串的字符数时,即count > tLen时,要尽可能的把窗口的开始位置往后移动,即start前移,使窗口尽可能的小。
在移动窗口位置时必须满足的条件:
(1)当前字符不在目标串T里
(2)已匹配的次数大于目标串里相应字符出现的次数
注意特殊情况的处理
(1)两个字符串都为空的时候
(2)S的长度<T的长度
(3)S字符都不在T里面时start的处理
class Solution {
public:
string minWindow(string S, string T) {
if(S == "" || T == "" ||S.length() < T.length()) return "";
int sLen =S.length(), tLen = T.length();
unordered_map<char,int> hashMap;
for(int i = ; i < tLen; ++ i) hashMap[T[i]]++;
unordered_map<char,int> findHashMap;
int count = , start =, minLen = INT_MAX,minLenStart = -;
for(int i = ; i < sLen; ++ i){
char ch = S[i];
if(hashMap.find(ch)==hashMap.end()) continue;
findHashMap[ch]++;
if(findHashMap[ch] <= hashMap[ch]) count++;
if(count>=tLen){
char c = S[start];
while(findHashMap.find(c) == findHashMap.end() || findHashMap[c] > hashMap[c] ){
if(findHashMap.find(c)!=findHashMap.end()) findHashMap[c]--;
start++;
c = S[start];
}
int len = i-start+;
if(len < minLen){
minLen = len;
minLenStart = start;
}
}
}
if(minLenStart == -)return "";
else return S.substr(minLenStart,minLen);
}
};
Leetcode Minimum Window Substring的更多相关文章
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [leetcode]Minimum Window Substring @ Python
原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...
- [LeetCode] Minimum Window Substring 散列映射问题
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- [Leetcode] minimum window substring 最小字符窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- LeetCode()Minimum Window Substring 超时,但觉得很清晰。
我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- 【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 ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
随机推荐
- [Unity3d]游戏中子弹碰撞的处理
如果使用Collider+Rigidbody的方式来处理,则它是每一帧进行判定碰撞:如果子弹过快导致碰撞发生在2帧之间,则会导致无法捕获这个碰撞效果 基于上述原因,我们要使用射线Raycast进行子弹 ...
- js 常用函数收集(基础)
(1).判断是否为数值 function isNum(obj){ return !isNaN(parseFloat(obj)) && isFinite(obj); } (2).判断是否 ...
- 汤森路透为何一定要卖掉SCI?
http://health.sohu.com/20160714/n459331727.shtml
- R语言学习笔记-机器学习1-3章
在折腾完爬虫还有一些感兴趣的内容后,我最近在看用R语言进行简单机器学习的知识,主要参考了<机器学习-实用案例解析>这本书. 这本书是目前市面少有的,纯粹以R语言为基础讲解的机器学习知识,书 ...
- 简单的方向传感器SimpleOrientationSensor
SimpleOrientationSensor是一个简单的方向传感器.能够识别手机如下表的6种方向信息: SimpleOrientation枚举变量 方向 NotRotated 设备未旋转 Rotat ...
- 实用框架(iframe)代码
<iframe src="http://www.baidu.com" marginwidth="0" marginheight="0" ...
- iOS应用动态部署方案
iOS的动态部署能极大的节约成本.苹果的审核周期很长,有的时候,你可能不得不等待将近2个星期去上架你的新功能或者bug.所以动态部署是有价值的. 我这里讨论的情况不把纯web应用考虑在内,因为用户体验 ...
- javascript数据结构-链表
gihtub博客地址 链表 是一种物理存储单元上非连续.非顺序的存储结构,它既可以表示线性结构,也可以用于表示非线性结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每 ...
- 值得推荐的android开源框架
1.volley 项目地址https://github.com/smanikandan14/Volley-demo (1) JSON,图像等的异步下载: (2) 网络请求的排序(scheduling) ...
- spark streaming 接收 kafka 数据java代码WordCount示例
http://www.cnblogs.com/gaopeng527/p/4959633.html