题目

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.

题解:

这道题也是用滑动窗口的思想,思想跟 Substring with Concatenation of All Words是一样的,同样是利用HashMap来存Dict,然后来遍历整个母串。因为这里是要求最短的包含子串的字符串,所以中间是可以允许有非子串字符的,当遇见非子串字符而count又没到子串长度时,可以继续走。

当count达到子串长度,说明之前遍历的这些有符合条件的串,用一个pre指针帮忙找,pre指针帮忙找第一个在HashMap中存过的,并且找到后给计数加1后的总计数是大于0的,判断是否为全局最小长度,如果是,更新返回字符串res,更新最小长度,如果不是,继续找。

这道题的代码也参考了code ganker的。

代码如下:

 1 public String minWindow(String S, String T) {
 2     String res = "";
 3     if(S == null || T == null || S.length()==0 || T.length()==0)
 4         return res;
 5     
 6     HashMap<Character, Integer> dict = new HashMap<Character, Integer>();
 7     for(int i =0;i < T.length(); i++){
 8         if(!dict.containsKey(T.charAt(i)))
 9             dict.put(T.charAt(i), 1);
         else
             dict.put(T.charAt(i), dict.get(T.charAt(i))+1);
     }
     
     int count = 0;
     int pre = 0;
     int minLen = S.length()+1;
     for(int i=0;i<S.length();i++){
         if(dict.containsKey(S.charAt(i))){
             dict.put(S.charAt(i),dict.get(S.charAt(i))-1);
             if(dict.get(S.charAt(i)) >= 0)
                 count++;
                 
             while(count == T.length()){
                 if(dict.containsKey(S.charAt(pre))){
                     dict.put(S.charAt(pre),dict.get(S.charAt(pre))+1);
                     
                     if(dict.get(S.charAt(pre))>0){
                         if(minLen>i-pre+1){
                             res = S.substring(pre,i+1);
                             minLen = i-pre+1;
                         }
                         count--;
                     }
                 }
                 pre++;
             }
         }//end for if(dict.containsKey(S.charAt(i)))
     }
     return res;
 }

Reference:

http://blog.csdn.net/linhuanmars/article/details/20343903

Minimum Window Substring leetcode java的更多相关文章

  1. Minimum Window Substring @LeetCode

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

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

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

  4. 53. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  5. leetcode76. Minimum Window Substring

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

  6. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

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

  8. [Leetcode][JAVA] Minimum Window Substring

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

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

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

随机推荐

  1. CSUOJ 1021 组合数末尾的零 二进制

    Description 从m个不同元素中取出n (n ≤ m)个元素的所有组合的个数,叫做从m个不同元素中取出n个元素的组合数.组合数的计算公式如下: C(m, n) = m!/((m - n)!n! ...

  2. FGPA 中的计数器Verilog语言(时钟分频器)

    在quartusII8.0中为ALTERAFPGA设置一个分频器(计数器) 输入时钟48Mhz 输出时钟9600HZ /* 实验名称: 计数器 ** 程序功能: 将48Mhz的时钟分频为9600Hz ...

  3. Keystone几种token生成的方式分析

    从Keystone的配置文件中,我们可见,Token的提供者目前支持四种. Token Provider:UUID, PKI, PKIZ, or Fernet 结合源码及官方文档,我们用一个表格来阐述 ...

  4. 设计模式 结构型模式 外观模式(Facade Pattern)

    在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化. 这时为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作 ”门面“ ...

  5. bzoj 5055: 膜法师 -- 树状数组

    5055: 膜法师 Time Limit: 10 Sec  Memory Limit: 128 MB Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇 ...

  6. python开发_tkinter_菜单的不同选项

    python的tkinter模块中,菜单也可以由你自定义你的风格 下面是我做的demo 运行效果: ====================================== 代码部分: ===== ...

  7. Codeforces Round #222 (Div. 1) D. Developing Game 扫描线

    D. Developing Game 题目连接: http://www.codeforces.com/contest/377/problem/D Description Pavel is going ...

  8. MVC的Action上下文:ActionExecutingContext

    就上图来看,大家注意了吗,ActionExecutingContext对象一共有3处引用.下面我来一一解析: 调用base.OnActionExecuting(filterContext)这个后,才会 ...

  9. Hibernate-数据库更新操作

    /* Session接口下操作存在以下问题: 数据更新操作: 1.更新的时候必须要有主键; 2.若只更新部分字段内容,则未设置的字段将被设置为Null(全表更新) 3.update()没有返回值,即不 ...

  10. JavaScript Promises

    上篇文章介绍了JavaScript异步机制,请看这里. JavaScript异步机制带来的问题 JavaScript异步机制的主要目的是处理非阻塞,在交互的过程中,会需要一些IO操作(比如Ajax请求 ...