Level:

  Hard

题目描述:

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.

思路分析:

知识点:滑动窗口

  关键点一:像这种找子串的可以使用滑动窗口Sliding Window来做。这个窗口由left和right来约束[left,right]这个闭区间,刚开始知识left=right=0,然后left不变,right不断增长,直到到达某个值,left和right一起增长,这样就实现了窗口的创建和向下滑动。

  关键点二:对于p中出现的字符以及次数需要记下来。可以使用一个数组ascaiiNums,长度为256,是因为ascaii一共只有256个,初始化的值表示角标对应的ascaii字符在p中出现的次数,那么没有出现过的就是0.之后如果s中出现过该字符,那么数组对应位置的值就减一。如果减之后的值大于等于0,就说明p中该字符出现了一次,这时p中未出现的字符数量count就减一。之所以要判断是>=0,是因为如果数组中原来的值是0(说明p中没有出现),那么减一后就成了负的,这种情况下p中剩下未出现的字符的count数值保持不变。如果count的值为0,说明p中的字符全出现了,找到一个符合条件的子串,left就是子串的首地址。之后要向右滑动窗口,移动的方法是left++,right++,更新p中未出现的字符个数count。

代码:

public class Solution{
public String minWindow(String s,String t){
if(s==null||t==null||s.length()==0||t.length()==0)
return null;
String res="";
int []map=new int [256];
int match=t.length();
int left=0; //窗口左端
for(int i=0;i<t.length();i++){
map[t.charAt(i)]++;
}
int minlen=Integer.MAX_VALUE;
for(int i=0;i<s.length();i++){//i充当窗口的右端
map[s.charAt(i)]--;
if(map[s.charAt(i)]>=0)
match--; //证明该字符在t中
if(match==0){
while(map[s.charAt(left)]<0){
map[s.charAt(left)]++;
left++; //这些字符都不存在与t中
}
if((i-left+1)<minlen){//更新最短子串
minlen=i-left+1;
res=s.substring(left,i+1);
}
match++; //寻找下一个包含t的串
map[s.charAt(left)]++;
left++;
}
}
return minlen==Integer.MAX_VALUE?"":res;
}
}

72.Minimum Window Substring(最小子串窗口)的更多相关文章

  1. lintcode 中等题:minimum window substring 最小子串覆盖

    题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

  2. [Leetcode] minimum window substring 最小字符窗口

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

  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] Minimum Window Substring 最小窗口子串

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

  5. Minimum Window Substring, 包含子串的最小窗口,双指针

    问题描述:给定字符串S,子串T,求S中包含T的最小窗口 Given a string S and a string T, find the minimum window in S which will ...

  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. 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...

  8. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  9. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

随机推荐

  1. python面向对象--item方法

    class Foo: def __getitem__(self, item): print("getitem") return self.__dict__[item] def __ ...

  2. 【转】SIP协议 会话发起协议

    转自:https://www.cnblogs.com/gardenofhu/p/7299963.html 会话发起协议(SIP)是VoIP技术中最常用的协议之一.它是一种应用层协议,与其他应用层协议协 ...

  3. hdu4417 Super Mario (树状数组/分块/主席树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个长度为n的序列,有m个询问,每次询问包含l,r,h,即询问区间[l,r]小于等 ...

  4. [python 学习] sys模块

    sys.stdout sys.stdin

  5. Tenka1 Programmer Contest D - Crossing

    链接 Tenka1 Programmer Contest D - Crossing 给定\(n\),要求构造\(k\)个集合\({S_k}\),使得\(1\)到\(n\)中每个元素均在集合中出现两次, ...

  6. CSS3订单提交按钮Loading代码

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...

  7. EhCache缓存框架的使用

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 我们使用EhCache缓存框架主要是为了判断重复Url,每次爬取一个网 ...

  8. win10半夜自动开机的问题分析

    win10半夜自动开机的系统日志: 解决方法一: 1.根据日志判断自动唤醒后,windows更新了时间和代理 服务管理器中,关闭windows update, 但是半夜还会自动开 再关闭服务管理器的w ...

  9. -bash: ./hello.jar: 无法执行二进制文件

    在linux中直接调用java包产生的 解决:依赖多个包要用冒号分隔,而不是分号 正确:> java -cp ./lib/*:./hello.jar hello 错误:> java -cp ...

  10. 思维导图初体验——openstack

    RABBITMQ memcache keystone glance nova neutron horizon cinder ceph