题目描述

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.

找出包含T所有字母的最小窗口。

注意:S=bba t=ba。此时right=2时,left=0,移动left时会++m[b],但是此时窗口内仍旧有一个b。所以在right移动时,m[b]=-1,即只要包含即--,而只有m[b]>=0时,才是有效的,才会++count。同样,在移动left时也是,只有m[b]>0时,才会count--。

 
这道题是字符串处理的题目,和Substring with Concatenation of All Words思路非常类似,同样是建立一个字典,然后维护一个窗口。区别是在这道题目中,因为可以跳过没在字典里面的字符(也就是这个串不需要包含且仅仅包含字典里面的字符,有一些不在字典的仍然可以满足要求),所以遇到没在字典里面的字符可以继续移动窗口右端,而移动窗口左端的条件是当找到满足条件的串之后,一直移动窗口左端直到有字典里的字符不再在窗口里。在实现中就是维护一个HashMap,一开始key包含字典中所有字符,value就是该字符的数量,然后遇到字典中字符时就将对应字符的数量减一。算法的时间复杂度是O(n),其中n是字符串的长度,因为每个字符再维护窗口的过程中不会被访问多于两次。空间复杂度则是O(字典的大小),也就是代码中T的长度。代码如下:

 class Solution {
public:
string minWindow(string S, string T) {
if(S.length()==)
return "";
map<char, int> m;
for(int i=;i<T.length();i++){
if(m.find(T[i])!=m.end())
m[T[i]]++;
else
m[T[i]]=;
}
int left=,right=,min=S.length()+,minStart=,count=;
for(right=;right<S.length();right++){
if(m.find(S[right])==m.end())
continue;
m[S[right]]--;
if(m[S[right]]>=){
count++;
}
while(count==T.length()){
if((right-left+)<min){
min=right-left+;
minStart=left;
}
if(m.find(S[left])!=m.end()){
m[S[left]]++;
if(m[S[left]]>)
count--;
}
left++;
}
}
string res="";
if(min!=S.length()+){
res=S.substr(minStart,min);
}
return res;
}
};

LeetCode-Minimum Window Substring -- 窗口问题的更多相关文章

  1. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  2. [Leetcode] minimum window substring 最小字符窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  3. [leetcode]Minimum Window Substring @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...

  4. [LeetCode] Minimum Window Substring 散列映射问题

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  5. Leetcode Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  6. LeetCode()Minimum Window Substring 超时,但觉得很清晰。

    我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...

  7. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

  8. 【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 ...

  9. 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 ...

  10. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

随机推荐

  1. 转载: CentOS/Linux 解决 SSH 连接慢

    CentOS/Linux 解决 SSH 连接慢 现在连接Linux服务器一般都是使用SSH远程连接的方式.最近新装了一台服务器,发现telnet时速度很快,ping时一切也正常,但SSH连接的时候却很 ...

  2. SQL Server中JOIN的用法

    JOIN 分为:内连接(INNER JOIN).外连接(OUTER JOIN).其中,外连接分为:左外连接(LEFT OUTER JOIN).右外连接(RIGHT OUTER JOIN).全外连接(F ...

  3. OutputDebugString方便格式化WIN32封装

    void TRACE(LPCTSTR lpszFmt, ...) { va_list args; va_start(args, lpszFmt); ; TCHAR *lpszBuf = (TCHAR* ...

  4. 牛客网 暑期ACM多校训练营(第二场)D.money-贪心 or 动态规划

    D.money 贪心,直接贴官方的题解吧. 题目大意 你要按照顺序依次经过n个商店,每到达一个商店你可以购买一件商品,也可以出售你手中的商品. 同一时刻你手上最多拿一件商品.在第i个商店购买和出售的代 ...

  5. ZOJ 3937 More Health Points (2016 浙江省赛 B题,可持久维护凸壳)

    题目链接  2016 ZJCPC Problem B 题意  CF 660F的树上版本. 其他做的方法都差不多,关键是把凸壳放到树上. 每次确定扔掉几个元素的时候直接$O(1)$修改(先不清楚这个位置 ...

  6. [POI2014]Solar Panels

    题目大意: $T(T\le1000)$组询问,每次给出$A,B,C,D(A,B,C,D\le10^9)$,求满足$A\le x\le B,C\le y\le D$的最大的$\gcd(x,y)$. 思路 ...

  7. 实现微信朋友圈点击评论按钮时cell上移

    实现场景:微信朋友圈TableView(BigTableView)的每一行cell都包含一个SmallTableView(显示所有点赞及评论) 实现思路: //BigTableView的content ...

  8. 合理配置SQL Server的最大内存

    http://blog.itpub.net/26435490/viewspace-1481846/

  9. Word中如何公式居中标号右对齐

    1.鼠标居中 2.插入一行三列表格 3.选中第一个表格,右键-表格属性-单元格-选项:然后回到单元格设置垂直居中,宽度为15%,同理第三个单元格,不过中间单元格也要设置,宽度为70%,这个word没有 ...

  10. JAVA常见算法题(五)

    package com.xiaowu.demo; /** * 利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. * * * @ ...