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 ...
随机推荐
- [小程序]那些icons
摘要 为了提供更友好的提示信息,会使用icon+信息的方式,向用户提示当前操作的成功,失败或者一些警告信息.小程序也为我们定义了一些icons,足够大部分情况的使用了. 那些icons 我们新建一个名 ...
- diff和patch的使用、patch文件的格式解说
为了弄懂 patch中的 p0 p1 和.orig文件是啥,找到了这篇文章! 来源:http://www.cnblogs.com/super119/archive/2010/12/18/19 ...
- Shell case esac语句
case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构. case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令.case语句格式如下: ...
- JS中数组Array的用法{转载}
js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^var arr = new Array();arr[0] = "aaa";arr[1] ...
- JavaScript学习链接
Js中this的用法:http://www.cnblogs.com/RitaRichard/archive/2011/10/14/2212161.html JavaScript\ActionScrip ...
- 限制帐号同时两处以上登录-ASP.NET
///登录页面 Hashtable haol = (Hashtable)Application["olTable"]; if (haol == null) { haol = new ...
- Hide JSP error icons in Eclipse
down voteaccepted Can can either configure this at workspace level or overwrite at web project level ...
- 网页设计之jQuery
1.在html中引入css和jQuery <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 读书笔记<白帽子讲web安全>
2016年3月24日 09:34:32 星期四 ddos攻击: 一种: 随机生成ip, 去建立链接, 由于http/tcp握手协议原理, 发送应答报文时因为ip无效会导致等待重发, 这种行为可以通过电 ...
- 红黑树/B+树/AVL树
RB Tree 红黑树 :http://blog.csdn.net/very_2/article/details/5722682 Nginx的RBTree实现 :http://blog.csdn ...