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. C语言程序设计--类型转换

    变量定义 int var_int = 111111111; char var_char = '2'; double var_double = 3.30; float var_float = 4.80; ...

  2. mvn deploy命令上传包

    需求:有的时候需要单独上传release jar包,因为存在工程代码在A内网SVN,Nexus在B内网.这种情况下使用VPN也无法解决Jar包发布的问题. 这个时候采取的方式只能是: 打出jar包 - ...

  3. VC/MFC程序开启关闭和打开自己或其他软件,更改窗口类

    一. 关闭自身软件 直接在需要关闭的位置输入 HANDLE hself = GetCurrentProcess(); TerminateProcess(hself, 0); 二.关闭其他软件 流程: ...

  4. Windows Server 2008 R2之五操作主控的管理

    一.概述 操作主控(FSMO)也称作操作主机(OM),它是指在AD中一个或多个特殊的DC,用来执行某些特殊的功能(资源标识符SID分配.架构修改.PDC选择等). 1.操作主控的分类 基于森林的操作主 ...

  5. Django---应用如何创建

    创建好的项目之后,需要创建各个应用模块: 创建方法: 就可以看到:index 应用

  6. POJ_3368_Frequent values

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19998   Accepted: 7180 ...

  7. HTML_css选择器

    第二种增加css样式的方法,可以在head中增加style标签,style中通过选择器定位标签增加css样式 CSS选择器分为六种: 1.id选择器 2.class选择器   3.标签选择器   4. ...

  8. 几种outofmemory

    几种outofmemory的解决方法:1.  java.lang.OutOfMemoryError: PermGen space PermGen space的全称是Permanent Generati ...

  9. js-jquery-SweetAlert2【一】使用

    概述:SweetAlert2是SweetAlert-js的升级版本,它解决了SweetAlert-js中不能嵌入HTML标签的问题,并对弹出对话框进行了优化,同时提供对各种表单元素的支持,还增加了5种 ...

  10. phpStudy安装

    以下一直默认安装 访问地址:http://127.0.0.1/vue/2.html