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 ...
随机推荐
- 搭建FTP服务器
yum install vsftpd -yyum install pam* db4* --skip-broken –y 创建并生成vsftpd 数据库文件vi /etc/vsftpd/ftpusers ...
- Container View 使用小技巧
一.传值,顺传 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { TVC *vc = segue.destin ...
- 14个技巧助你适配 iOS10
1.Notification(通知) 自从 Notification 被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到 Us ...
- 大熊君JavaScript插件化开发------(第一季)
一,开篇分析 Hi,大家!大熊君又来了,今天这系列文章主要是说说如何开发基于“JavaScript”的插件式开发,我想很多人对”插件“这个词并不陌生, 有的人可能叫“组件”或“部件”,这不重要,关键是 ...
- 【转载】使用Pandas创建数据透视表
使用Pandas创建数据透视表 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas创建数据透视表 目录 pandas.pivot_table() 创建简单的数据透视表 增加一个行维度(inde ...
- [Python] 目录和文件操作
在Linux系统下用Python写脚本,肯定不能避免各种与目录和文件夹有关的操作.为了以后方便查阅,简单地针对Python中与目录和文件夹有关的操作进行汇总. 需要实现导入的模块为: import o ...
- js创建,获取,检测cookie
- Idea 开发 web项目
1.经历 很久没有搞 web 项目了,最近一段时间搞过很多次了,但是总是在 mac 上部署失败. 2.方法: 用idea 新建一个模板的 Spring MVC 项目,部署就可以了. 3.参考: htt ...
- JavaScript访问ab页面定时跳转代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- embed标签loop=true背景音乐无法循环
在html网页中加入背景音乐并设置为循环播放.一开始用<embed>标签,设置loop="true", 但是结果发现在IE浏览器可以,但是在chrome浏览器却无法实现 ...