题目

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. NetCore+Dapper WebApi架构搭建(二):底层封装

    看下我们上一节搭建的架构,现在开始从事底层的封装 1.首先需要一个实体的接口IEntity namespace Dinner.Dapper { public interface IEntity< ...

  2. 你见过这些JavaScript的陷阱吗?

    一.成员操作符 Number.prototype.testFn=function () { console.log(this<0, this.valueOf()); } var num = -1 ...

  3. 1019 General Palindromic Number (20)(20 point(s))

    problem A number that will be the same when it is written forwards or backwards is known as a Palind ...

  4. android studio 汉化 个性化 美化 快速操作项目 目录

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...

  5. Windows Server 2008 R2下将nginx安装成windows系统服务

    一直在Linux平台上部署web服务,但是最近的一个项目,必须要用windows,不得已再次研究了nginx在windows下的表现,因为Apache httpd在Windows下表现其实也不算太好, ...

  6. play framework系列之打包war

    概览 Play framwork 是我们一直在使用的框架,从刚开始的简单使用,乱起八糟的jar包引用,项目组成员之间的下载项目之后的引用问题等,遇到各种问题,我都一一解决,我将在这个系列中奉上解决方案 ...

  7. CSS实现背景透明,文字不透明

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Git_管理修改

    现在,假定你已经完全掌握了暂存区的概念.下面,我们要讨论的就是,为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件. 你会问,什么是修改?比如你新增了一行,这就是一个修改 ...

  9. Flash Builder 4的快捷方式和调试技巧

    Flash Builder 4的快捷方式和调试技巧 来自于flex开发人员中心:http://www.adobe.com/cn/devnet/flex/articles/flashbuilder_sh ...

  10. WPF中删除打开过的图片

    在WPF中,当我们删除打开过的图片时,往往会遇到"...无法删除,文件正在被另一个进程使用"的异常.即使当前文件是打开后关闭过的也不行. 这个问题的原因很简单,是因为WPF的缓存策 ...