【leetcode刷题笔记】Minimum Window Substring
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).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
Note:
If there is no such window in S that covers all characters in T, return the emtpy string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
题解:参考leetcode给出的解答,实现了O(n)的算法。
用到的主要变量如下:
设置一个Map:ToFind记录T中出现的字符的种类和个数,我们需要在S中找到这些字符。
设置另一个Map:hasFound记录当前窗口中包含的字符的种类和个数。
这两个Map,结合一个变量count——在当前窗口中找到的T中的字符个数,我们就可以判断当前窗口是否包含T中所有的字符了。
算法的主要过程如下:
- 当前窗口的起始和结束位置都在S(0)处;
- 当窗口中没有包含T中所有的字符时(count<T.length),扩展窗口的右端end,直到窗口中包含了T中所有的变量。
- 此时窗口不一定是最小的,因为左端还有可能缩进,根据窗口的左端变量begin所指的元素,把窗口的左端尽可能右移。
- 得到一个包含T的窗口,跟最小的窗口比较,如果比最小的窗口小,就更新最小的窗口。
举个例子:S = "acbbaca" , T = "aba"。

如上图所示,end从初始的位置扩展到下面的图中的位置时候,窗口包含了T中所有的字符,而且begin也无法挪动了,此时得到一个最小窗口长度为5;

接下来继续移动end直到下一个包含在T里面的元素处(见第二幅图),然后把begin尽可能往右移动(见第三幅图),得到一个新的当前最小窗口baca,由于它比最小窗口acbba短,所以更新最小窗口为baca。算法结束。
所以我们可以看出begin只有在找到T的时候才右移收缩窗口,而end一直后移。在找到第一个窗口后,end每移动到一个T里面包含的元素处,就会有一个新的窗口(比如上述end从索引为4的地方挪动到索引为6的地方)。
代码如下:
public class Solution {
public String minWindow(String S, String T) {
HashMap<Character, Integer> needToFind = new HashMap<Character, Integer>();
HashMap<Character, Integer> hasFound = new HashMap<Character, Integer>();
int count = 0;
for(int i = 0;i < T.length();i++){
char ch_t = T.charAt(i);
if(!needToFind.containsKey(ch_t)){
needToFind.put(ch_t, 1);
hasFound.put(ch_t, 0);
}
else {
needToFind.put(ch_t, needToFind.get(ch_t)+1);
}
}
int minWindowBegin = -1;
int minWindowEnd = S.length();
int minWindowLen = S.length();
for(int begin = 0,end = 0; end < S.length();end++){
char char_end = S.charAt(end);
//skip character not in T
if(!needToFind.containsKey(char_end))
continue;
hasFound.put(char_end, hasFound.get(char_end)+1);
if(hasFound.get(char_end) <= needToFind.get(char_end))
count++;
if(count == T.length()){
//narrow down the window as much as possible
char char_begin = S.charAt(begin);
while(!needToFind.containsKey(char_begin) || hasFound.get(char_begin) > needToFind.get(char_begin)){
if(needToFind.containsKey(char_begin) && hasFound.get(char_begin) > needToFind.get(char_begin)){
hasFound.put(char_begin, hasFound.get(char_begin)-1);
}
begin++;
char_begin = S.charAt(begin);
}
int windowLen = end - begin + 1;
if(windowLen <= minWindowLen){
minWindowBegin = begin;
minWindowEnd = end;
minWindowLen = windowLen;
}
}
}
if(count == T.length()){
StringBuffer sbBuffer = new StringBuffer();
for(int i = minWindowBegin;i<=minWindowEnd;i++)
sbBuffer.append(S.charAt(i));
return sbBuffer.toString();
}
else
return "";
}
}
【leetcode刷题笔记】Minimum Window Substring的更多相关文章
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- 【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- LeetCode(76) Minimum Window Substring
题目 Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- Windows中搭建ftp服务器
使用工具Quick Easy FTP Server Windows中搭建FTP服务器有什么用呢? 确实没有用,直到有一次,我在VM中安装了Linux虚拟机,但是文件怎么也上传不到这个虚拟机中. 然后用 ...
- Spring MVC生成JSON数据
以下示例演示如何使用Spring Web MVC框架生成JSON数据格式.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: ...
- 创建一个视图JSP文件的helloWorld.jsp
让我们创建下面的JSP文件的helloWorld.jsp,在WebContent文件夹在你的eclipse项目.要做到这一点,右键单击WebContent文件夹中的项目资源管理器,然后选择“新建”&g ...
- windows无法设置防火墙
无法打开防火墙,出现错误代码0x80070422,解决方法:windows开始键->运行->调出运行窗口,输入services.msc,(或者控制面板->管理工具->服务)找到 ...
- 第0周---python网络爬虫前奏
目标:掌握定向网络数据爬取和网页解析的基本能力 Python开发工具的选择
- 导出大量数据到excel表
set_time_limit(0);error_reporting(0); // 输出Excel文件头,可把user.csv换成你要的文件名header('Content-Type: applicat ...
- MVC架构模式概述
MVC MVC概述: Model–view–controller (MVC) is a software architectural pattern for implementing user int ...
- kubectl工具的windows安装方法
1.首先安装Chocolatey 参考:https://chocolatey.org/install#install-with-powershellexe windows7+以上操作系统的cmd sh ...
- Java中Solr集群的测试
import org.apache.solr.client.solrj.impl.CloudSolrServer; import org.apache.solr.common.SolrInputDoc ...
- python函数的学习笔记
这篇文章是我关于学习python函数的一些总结 一.随着函数的引入,这里首先要说的就是全局变量和局部变量了. 什么是全局变量.什么是局部变量: 全局变量就是全局都能调用的变量,一般都在文件的开头,顶头 ...