72.Minimum Window Substring(最小子串窗口)
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(最小子串窗口)的更多相关文章
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
- [Leetcode] minimum window substring 最小字符窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [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 ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Minimum Window Substring, 包含子串的最小窗口,双指针
问题描述:给定字符串S,子串T,求S中包含T的最小窗口 Given a string S and a string T, find the minimum window in S which will ...
- [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 ...
- 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
随机推荐
- alert(1) to win 8
function escape(s) { return '<script>console.log("' + s.toUpperCase() + '")</scri ...
- 设置div 居中 和得到dom元素高度
javascript中获取dom元素高度和宽度的方法如下: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网 ...
- 雷哥带你了解webSocket原理
偶然在知乎上看到一篇回帖,瞬间觉得之前看的那么多资料都不及这一篇回帖让我对 websocket 的认识深刻有木有.所以转到我博客里,分享一下.比较喜欢看这种博客,读起来很轻松,不枯燥,没有布道师的阵仗 ...
- 19 如何在String和Byte[]对象之间进行转换?
- 如何把word文档内容和图片直接导入到wordpress编辑器
Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...
- 【HDOJ6651】Final Exam(贪心)
题意:有n门课,价值之和为m,每门课的价值可能是0到m 一门价值为x的课需要花至少x+1时间准备才能通过 问不管价值如何分配都能通过至少k门课的最小总准备时间 m,n,k<=1e9 思路: #i ...
- python中的变量的学习
今年以来,时间都安排在学习python语言上了.(python3) 从最基础的变量开始学习.尽管以前学习过一段时间,但这次是系统性的学习,所以多花点时间,重新开始.学习完基础知识后,多练习几个项目. ...
- 初次打开jenkins页面一片空白的解决办法
安装完成jenkins后,点击[使用admin账号继续]后,页面一片空白的解决办法: step1: Jenkins插件管理-高级设置界面==> http://localhost:8080/plu ...
- NaN、null、undefined的那些事
一.NaN , NaN, 'abc', false] let newArray = JSON.parse(JSON.stringify(arr)) // [1, null, 'abc', false] ...
- DEDE网站地图优化技巧
DEDE网站地图优化技巧-把网站地图生成在系统根目录,利于搜索引擎收录相信恨多用DEDECMS做站的朋友,为避免将data目录内的东西随便外泄,在robots中将data目录屏蔽了,但是DEDE默认的 ...