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 ...
随机推荐
- 11月1日上午PHP------empty、 is_null、isset、unset的区别
1.empty 判断一个变量是否为"空".null.false.00.0.'0′.』.为以上值的变量在检测時都将返回true. 2.isset 判断一个变量是否已经设置.0.00. ...
- 多光谱图像数据库, Multispectral images databses
1. https://scien.stanford.edu/index.php/hyperspectral-image-data/ 2. http://www.cs.columbia.edu/CAVE ...
- 前端MVC框架、类库、UI框架选择
CSS预处理器sass(基于Ruby服务端版)less(客户端版:基于js; 服务端版:基于nodejs) 前端UI框架JqueryMiniUI: http://www.miniui.com/(适用于 ...
- 微博RPC框架motan入门笔记
Motan 是一套高性能.易于使用的分布式远程服务调用(RPC)框架. 功能 支持通过spring配置方式集成,无需额外编写代码即可为服务提供分布式调用能力. 支持集成consul.zookeeper ...
- 如何解决mathpage.dll或MathType.dll文件找不到问题
解决方法(具体图文教程): 步骤一 要确保路径被office信任.依次打开word->文件->选项->信任中心->信任中心设置->添加新位置,添加C:\Program F ...
- 【URLDecoder】java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in es
Java调用 URLDecoder.decode(str, "UTF-8"); 抛出以上的异常,其主要原因是% 在URL中是特殊字符,需要特殊转义一下, 上面的字符串中'%'是一个 ...
- CSS3 时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- R笔记 单样本t检验 功效分析
R data analysis examples 功效分析 power analysis for one-sample t-test单样本t检验 例1.一批电灯泡,标准寿命850小时,标准偏差50,4 ...
- 为C#自定义控件添加自定义事件
这里的自定义控件是由普通控件组合而成的. 希望事件响应代码推迟到使用自定义控件的窗体里写. 步骤一:新建一个用户控件,放两个按钮,Tag分别是btn1,btn2. 这两个按钮的共用单击事件处理代码如下 ...
- Gyro
Gyro 2 Plugin gyro2.jsVersion 1.19HTML5 only 本插件使用手机或平板设备上的陀螺仪和加速度传感器来控制krpano中的浏览和观看方向.Gyro2插件对比旧的陀 ...