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. 关于c#静态构造函数

    http://baike.baidu.com/view/2634573.htm?fr=aladdin 在百科上看到C#的新特性静态构造函数,其中提到静态构造函数“不能继承” 今天做了个试验,发现实际上 ...

  2. SQL SERVER With语法[转]

    今天在论坛上看到一个举例,关于sql server 的示例.1/25/50/100美分,多少种可能拼凑成2美元. 看了其中第一条语法,放在SQL SERVER中测试,发现真的列举出所有组合成2美元的方 ...

  3. jQuery的开始

    一.下载 jQuery http://jquery.com/download/ 二.什么是jQuery: 1.jQuery 是一个 JavaScript 库. 2.jQuery 极大地简化了 Java ...

  4. C# Using 用法

    using 语句允许程序员指定使用资源的对象应当何时释放资源.为 using 语句提供的对象必须实现 IDisposable 接口.此接口提供了 Dispose 方法,该方法将释放此对象的资源. 一起 ...

  5. APP如何实现推送功能

    一.推送功能的集成 (1)在Umeng开发者中心,申请新应用,开通推送功能.此时需要上传开发推送证书和生产推送证书的p12文件. 申请证书的流程如下: >>1 创建开发推送证书 >& ...

  6. Linq group

    using System;using System.Collections.Generic;using System.Linq; public class MyClass{ public static ...

  7. 新的框架,新的感觉ASP.NET MVC 分享一个简单快速适合新手的框架

    在ASP.NET世界中摸爬滚打好几年,用过了各种框架,在最初的ASP.NET web from 到现在的MVC 在起初的经典三层,到现在的MVC  IOC  注入 . 突然发现,有些时候真不是跟风用一 ...

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

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

  9. Asp.Net MVC4入门指南(9):查询详细信息和删除记录

    在本教程中,您将查看自动生成的Details和Delete方法. 查询详细信息和删除记录 打开Movie控制器并查看Details方法. public ActionResult Details(int ...

  10. rdlc报表DEMO

    rdlc报表demo  .net 4.0  vs2013 文本框,图像控件,checkbox样式的打印 下载链接