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.

使用两个指针start和end去截取S中的字符串,初始都为S头部。end指针先往后遍历,直到T中所有字符都出现在子字符串中或者到S结尾,这时候把start往后挪,尽量使子串长度小,直到再往后挪就会不满足T中字符都在子串中的条件或者到达end,这时候记录start和end并计算子串长度,如果比之前的小则更新start和end。如此反复这两步直到end到S末尾。

问题是两个关键时刻:1.T中所有字符都出现在子字符串中;2. 再往后挪就会不满足T中字符都在子串中的条件,该怎么被捕获。

使用两个HashMap可以实现。第一个HashMap, need记录关于T中所有字符的次数统计,在遍历S中作为标准,不会变化。第二个HashMap, found记录遍历过程中字符出现次数的动态变化。

条件2可以这么实现: 一旦发现found中当前字符(前提是存在于found中)的值已经等于need中的值,则停止start指针的遍历。否则可以继续遍历不过found中的值要-1。

条件1可以联系T的长度来实现:使用变量count记录已在S中找到的T中字符个数。一旦count等于T的长度,开始把start往后挪。那么,什么样的字符是已在S中找到的T中字符,可以算进count?依然使用found和need可以判断,如果found中字符的值小于等于need中它的值,说明这遍历到的字符应该算进count,因为关于这个字符,还没有或者刚刚好满足它应该出现的次数,应该要算作T中字符。而若found中的值大于need中的值,说明该已经遍历到足够多该字符,不需要再来这种字符了,需要的是T中其他的字符,所以不能算进count.

     public String minWindow(String S, String T) {
if(T.length()>S.length())
return "";
HashMap<Character, Integer> need = new HashMap<Character, Integer>();
HashMap<Character, Integer> found = new HashMap<Character, Integer>(); for(int i=0;i<T.length();i++) {
char t = T.charAt(i);
if(!need.containsKey(t)) {
need.put(t,1);
found.put(t,0);
} else
need.put(t, need.get(t)+1);
}
int start = 0;
int end = 0;
int minStart = -1;
int minEnd = S.length();
int count = 0;
for(;end<S.length();end++) {
char t = S.charAt(end);
if(need.containsKey(t)) {
found.put(t, found.get(t)+1);
if(found.get(t)<=need.get(t))
count++;
if(count==T.length()) {
for(;start<=end;start++) {
char t2 = S.charAt(start);
if(need.containsKey(t2)) {
if(found.get(t2)>need.get(t2))
found.put(t2, found.get(t2)-1);
else
break;
}
}
if(start>end)
start--;
if(end-start<minEnd-minStart) {
minStart = start;
minEnd = end;
}
}
}
}
return minStart==-1?"":S.substring(minStart,minEnd+1);
}

[Leetcode][JAVA] Minimum Window Substring的更多相关文章

  1. Java for LeetCode 076 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] 76. 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] 76. Minimum Window Substring 最小窗口子串

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

  4. [leetcode]76. Minimum Window Substring最小字符串窗口

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

  5. 【leetcode】Minimum Window Substring (hard) ★

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

  6. Leetcode#76 Minimum Window Substring

    原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...

  7. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  8. Minimum Window Substring @LeetCode

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

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

随机推荐

  1. 【POJ2482】Stars in Your Window(线段树,扫描线)

    题意:在二维坐标系中有一些带权值的点,要求用一个长宽指定不能互换的框套住其中的一些,使得它们的权值和最大. n<=10000 x,y<=2^31 思路:首先按X排序,将Y坐标离散化,X坐标 ...

  2. linux学习之——vim简明教程

    摘自  http://blog.csdn.net/niushuai666/article/details/7275406 ——————————正文开始—————————— 你想以最快的速度学习人类史上 ...

  3. Trie / Radix Tree / Suffix Tree

    Trie (字典树) "A", "to", "tea", "ted", "ten", "i ...

  4. const变量初始化问题

    在C++中const变量定义时必须进行初始化,否则无法通过编译. 初始化的方式有多种,可以通过字面值对其进行初始化,也可以通过变量对其初始化,或其他方式,只要能给const变量赋初值即可(当然初值应该 ...

  5. 大批量GPS坐标转百度坐标

    一. 百度地图API大批量转换时有数量限制,一个一个转.  用到的方法接口    /**      源坐标 格式:经度,纬度;经度,纬度… 最多支持100个;      源坐标类型:默认为1,即GPS ...

  6. C# 委托如何理解 打个比喻

    初学者可能会给winform窗体注册事件,也听过事件是基于委托实现的 那么,委托是什么,事件又是什么,委托和事件是什么关系. 个人喜欢做一些比喻,把这些东西想象成某一个模型,这样方便记忆,理解,随着对 ...

  7. Java汉字转拼音

    import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...

  8. java虚拟机运行时乱码问题

    问题: Android端通过socket发送文本到windows,windows调用系统剪切板进行粘贴的操作,java服务端在eclipse下直接运行粘贴的文本无乱码,打包jar后粘贴的文本乱码. 解 ...

  9. 如何让aspnet服务加载静态资源html(我的动态网页静态化) 转

    我们知道,IIS自身是不能处理像ASPX扩展名这样的页面,只能直接请求像HTML这样的静态文件. 当客户端请求一个服务器资源时,这个HTTP请求会被inetinfo.exe进程截获(www服务),然后 ...

  10. C# 在winform或者wpf中显示控制台窗口

    这儿需要使用两个系统函数: BOOL WINAPI FreeConsole(void); //// 关闭控制台窗口,参考:http://msdn.microsoft.com/en-us/library ...