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放进tMap中,同时创建一个保存T中所有字符(不重复)的图sMap,用一个计数器表示sMap和tMap的重合程度,一旦sMap包含了tMap即找到了一组解,通过收缩begin指针获取刚好包含tMap的位置,之后就是比较大小了,JAVA实现如下:

 public String minWindow(String s, String t) {
HashMap<Character, Integer> tMap = new HashMap<Character, Integer>();
HashMap<Character, Integer> sMap = new HashMap<Character, Integer>();
for (int i=0;i<t.length();i++){
sMap.put(t.charAt(i), 0);
if (!tMap.containsKey(t.charAt(i)))
tMap.put(t.charAt(i), 1);
else
tMap.put(t.charAt(i), tMap.get(t.charAt(i)) + 1);
}
int begin=0,count=0,minBegin=0,length=s.length()+1;;
for(int i=0;i<s.length();i++){
if(!tMap.containsKey(s.charAt(i)))
continue;
sMap.put(s.charAt(i), sMap.get(s.charAt(i))+1);
if(sMap.get(s.charAt(i))<=tMap.get(s.charAt(i)))
count++;
if(count==t.length()){
for(int j=begin;j<=i;j++){
if(!tMap.containsKey(s.charAt(j)))
continue;
if(sMap.get(s.charAt(j))>tMap.get(s.charAt(j))){
sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
continue;
}
sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
count--;
begin=j+1;
if(length>i-j){
length=i-j;
minBegin=j;
}
break;
}
}
}
return length!=s.length()+1?s.substring(minBegin, minBegin+length+1):"";
}

Java for LeetCode 076 Minimum Window Substring的更多相关文章

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

  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】Minimum Window Substring (hard) ★

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

  5. 076 Minimum Window Substring 最小窗口子字符串

    给定一个字符串 S 和一个字符串 T,找到 S 中的最小窗口,它将包含复杂度为 O(n) 的 T 中的所有字符.示例:S = "ADOBECODEBANC"T = "AB ...

  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. [Asp.net mvc] 在Asp.net mvc 中使用MiniProfiler

    MiniProfiler是Stack Overflow团队设计的一款性能分析的小程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控,监控内容包括数据库内容 ...

  2. __name__和__main的含义

    if __name__ == "__main__": main() This module represents the (otherwise anonymous) scope i ...

  3. int方法

    代码 #int内部功能 name='Kamil.Liu' age=18 num=-11 print(dir(age)) print(age.bit_length())#返回表示当前数字占用的最少位数 ...

  4. 【poj3714】 Raid

    http://poj.org/problem?id=3714 (题目链接) 现在才搞平面最近点对..感觉有点尴尬 题意 给出平面上两组点,每组n个,求两组点之间最短距离 Solution1 平面最近点 ...

  5. 友盟iOS推送配置(从真机调试到推送)

    下面我来讲解一下友盟iOS的推送配置,其实友盟只是一个示例,换做其余的第三方推送服务也会适用,只是第三方的后面服务变了而已. iOS推送(包括真机调试)所需要的步骤和文件如下: 备注:这里我将省略掉一 ...

  6. UvaLive 5026 Building Roads

    传送门 Time Limit: 3000MS Description There is a magic planet in the space. There is a magical country ...

  7. IOS基础之 (十五)知识点

    一 SEL 1. 方法的存储位置 每个类的方法地址列表都存储在类对象中. 每个方法都有一个与之对应的SEL类型的对象. 根据一个SEL对象就可以找到方法的地址,进而调用方法. Person.h #im ...

  8. SCU 4424(求子集排列数)

    A - A Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice ...

  9. spark.SecurityManager: SecurityManager: authentication disabled

  10. Nginx 中 fastcgi_pass 监听端口 unix socket和tcp socket差

    Nginx 中 fastcgi_pass 监听端口 unix socket和tcp socket差别   Nginx连接fastcgi的方式有2种:unix domain socket和TCP,Uni ...