题目

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. 洛谷——P1747 好奇怪的游戏

    P1747 好奇怪的游戏 题目背景 <爱与愁的故事第三弹·shopping>娱乐章. 调调口味来道水题. 题目描述 爱与愁大神坐在公交车上无聊,于是玩起了手机.一款奇怪的游戏进入了爱与愁大 ...

  2. maven 发布jar包到远程仓库

    有的时候我们需要发布一些自己写的相关jar包到maven私服,供项目组使用. 首先在setting.xml文件添加,这里 注意 要保证该账户有发布的权限 <servers> <ser ...

  3. ASP.NET MVC 提高运行速度的几种性能优化方法

    主要介绍ASP.NETMVC 应用提速的六种方法,因为没有人喜欢等待,所以介绍几种常用的优化方法. 大家可能会遇到排队等待,遇到红灯要等待,开个网页要等待,等等等. 理所当然,没有人喜欢等待网页慢吞吞 ...

  4. 1004 Counting Leaves (30)(30 point(s))

    problem A family hierarchy is usually presented by a pedigree tree. Your job is to count those famil ...

  5. loj#2718. 「NOI2018」归程

    题目链接 loj#2718. 「NOI2018」归程 题解 按照高度做克鲁斯卡尔重构树 那么对于询问倍增找到当前点能到达的高度最小可行点,该点的子树就是能到达的联通快,维护子树中到1节点的最短距离 s ...

  6. 浅谈期望的线性性(可加性)【CodeForces280c】【bzoj3036】【bzoj3143】

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=63399955 向大(hei)佬(e)势力学(di ...

  7. Socket通信原理简介

    Socket通信原理简介 字数1011 阅读1766 评论2 喜欢11 何谓socket 计算机,顾名思义即是用来做计算.因而也需要输入和输出,输入需要计算的条件,输出计算结果.这些输入输出可以抽象为 ...

  8. 【10.4校内测试】【轮廓线DP】【中国剩余定理】【Trie树+博弈】

    考场上几乎是一看就看出来轮廓线叻...可是调了两个小时打死也过不了手出样例!std发下来一对,特判对的啊,转移对的啊,$dp$数组竟然没有取max!!! 某位考生当场死亡. 结果下午又请了诸位dala ...

  9. BZOJ5137[Usaco2017 Dec]Standing Out from the Herd

    看了半天题 不知道怎么用SAM维护 于是借(chao)鉴(xi)的一发神犇的 只要判断这个子串之前被标记的记号(也就是他属于第几个串)和这次转移到的是否相同 如果不同就说明该子串属于多个串 直接标记- ...

  10. codevs 1063 合并果子 STL 优先队列

    1063 合并果子 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codevs.cn/problem/1063/ Description 在一 ...