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. 使用mimikatz获取和创建Windows凭据的工具和方法

    Mimikatz 下载地址 https://github.com/gentilkiwi/mimikatz/releases 本地凭据破解 以管理员身份运行(拿到shell提权后) mimikatz#p ...

  2. FileInputStream 和 FileOutputStream

    简介 FileInputStream和FileOutputStream都是用来处理二进制数据源磁盘文件的流的. 他们分别派生自顶层抽象类InputStream和OutputStream FileInp ...

  3. MapReduce的计数器

     第一部分.Hadoop计数器简述 hadoop计数器: 可以让开发人员以全局的视角来审查程序的运行情况以及各项指标,及时做出错误诊断并进行相应处理. 内置计数器(MapReduce相关.文件系统相关 ...

  4. 设置tabBar中间的按钮比较大的发布

    MainTabBarController.h UITabBarItem *item = [self.tabBar.items objectAtIndex:index]; [item setTitle: ...

  5. javaweb使用 window.location.href 传中文参数 乱码问题

    JS: var cn_name=  document.getElementById("cn_name"); window.location.href="${URL}?na ...

  6. SQL语句常见视图操作部分试题(一)

    创建一个名称为EMPLOYEES_VU的视图,它基于EMPLOYEES表中的雇员号.雇员名和部门号.将雇员名的列标题改为EMPLOYEE. CREATE VIEW EMPLOYEES_VU AS SE ...

  7. 14nm或于6月量产,中芯首次披露12nm及第二代FinFET "N+1"计划(详细数据)

    日前中芯国际公布2018年度第四季度业绩,实现营收7.88亿美元,14nm工艺进入客户验证阶段,可望于今年6月份量产,且12nm工艺开发取得突破. 根据中芯国际披露的财报,2018年第四季度实现营业收 ...

  8. 洛谷P1903 数颜色 [国家集训队] 莫队

    正解:带修莫队 解题报告: 可以理解为引入时间参数,然后就是有了仨参数,关于这个修改同样的是,如果时间是相同的,不用搞,如果时间不相同做一下时光倒流/时光推移就成嘛 但是肯定既然这样的话,按照原来的s ...

  9. Loadrunner之https协议录制回放报错如何解决?(九)

    一.录制中遇到报错27778的问题(如下图1),即关于录制的链接为https开头的问题,分两个步骤解决,如下: 图1 https访问报错解决步骤如下: 1.修改Vuser-->Run-time ...

  10. xls的读写

    import xlrd # 读取xls文件 # 打开xls文件 data = xlrd.open_workbook('x1.xls') print(type(data)) # 获取表Sheet1 ta ...