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

Example:

Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"

Note:

  • If there is no such window in S that covers all characters in T, return the empty string "".
  • If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
 class Solution {
public String minWindow(String s, String t) { if(s.length()<t.length())
return "";
Map<Character,Integer> wordDict = constructWordDict(t); int slow =0,minLen=Integer.MAX_VALUE,fast=0,matchCount=0,start = 0;
for(fast=0;fast<s.length();fast++){
char ch = s.charAt(fast);
Integer cnt = wordDict.get(ch); //当前字符不在map中,fast++
if(cnt ==null)
continue;
wordDict.put(ch,cnt-1); //当前字符在map中,且 cnt==1,需要这个match, match总数++
if(cnt==1)
matchCount++; // 如果 match的个数够了,尝试移动slow,使其更短
while(matchCount==wordDict.size()){
//更新最短长度
if(fast-slow+1<minLen){
minLen = fast-slow+1;
start=slow;
}
//移动slow
char left = s.charAt(slow++);
Integer leftCnt = wordDict.get(left);
//当 slow 对应的字符串不在map中,说明当前字符串不需要match,继续移动
if(leftCnt==null)
continue; //当slow 对应的字符串在map中时,map中的key+1,
wordDict.put(left,leftCnt+1);
//如果slow对应的元素cnt==0,说明移动过头了,需要重新match slow对应的元素
if(leftCnt==0)
matchCount --; }
}
return minLen==Integer.MAX_VALUE?"":s.substring(start,start+minLen);
}
private Map<Character,Integer> constructWordDict(String s){
Map<Character,Integer> map = new HashMap<>();
for(char ch :s.toCharArray()){
Integer cnt = map.get(ch);
if(cnt==null)
map.put(ch,1);
else
map.put(ch,cnt+1);
}
return map;
}
}

https://www.youtube.com/watch?v=9qFR2WQGqkU

76. Minimum Window Substring(hard 双指针)的更多相关文章

  1. 刷题76. Minimum Window Substring

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

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

  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. 76. Minimum Window Substring

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

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

  7. 76. Minimum Window Substring (JAVA)

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

  8. [LC] 76. 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】#76. Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. ELK系列三:Elasticsearch的简单使用和配置文件简介

    1.定义模板创建索引: 首先定义好一个模板的例子 { "order":14, "template":"ids-1", "state ...

  2. Artech的MVC4框架学习——第二章URL路由

    总结:HttpModule 和HttpHandler是Asp.net管道的两个重要组件.请求最终处理通过HttpHandler完成.MVC就是通过名为MvcHandler自定义HttpHandler现 ...

  3. MAC SVN 基本设置 终端命令

    extends:http://www.cnblogs.com/heiniuhaha/archive/2012/07/31/2616493.html 安装XCode后Mac OS X 系统已经内置了sv ...

  4. svn和git的优缺点

    git官网api: https://git-scm.com/docs 一. 集中式vs分布式 1. Subversion属于集中式的版本控制系统集中式的版本控制系统都有一个单一的集中管理的服务器,保存 ...

  5. mysql 外键 cascade

    1 . cascade方式在父表上update/delete记录时,同步update/delete掉子表的匹配记录 2. set null方式在父表上update/delete记录时,将子表上匹配记录 ...

  6. vim配置函数跳转(c/c++)

    暂时草记一下,有时间好好整理 ctags 如果只是查看函数与变量是在哪里定义的,用ctags就可以了. ctrl+]跳到定义的地方,ctrl+t跳回来. 想要像IDE那样在旁边显示函数与变量列表,用t ...

  7. XCache 一种快速可靠的PHP操作码缓存

    1,错误报告开启 错误报告是在PHP中一个非常有用的功能,应同时在开发阶段启用. 这可以帮助我们确定我们的代码中的问题. 最常用的功能是“E_ALL”,这有助于我们发现所有的警告和严重错误. 必须指出 ...

  8. confirm

    注意confirm是window对象的方法,当确认时,返回true,取消时,返回false

  9. Gym-101375C MaratonIME eats japanese food 初始化struct技巧

    题意:两种操作:A ,R 分别有三个参数,表示放或者移走(x,y)处半径为r的盘子.5000次操作,每次操作判断是否可行. 题解:两种情况不行:1.相交2.放到桌子外面.5000的数据直接模拟,删除操 ...

  10. Python高阶函数:map、reduece、filter

    笔记中函数简介: map函数:遍历序列,对序列中每个元素进行操作,最终获取新的序列. reduce函数:对于序列内所有元素进行累计操作. filter函数:对于序列中的元素进行筛选,最终获取符合条件的 ...