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的更多相关文章

  1. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

  2. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  3. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

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

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

  6. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  7. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  8. 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 ...

  9. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

随机推荐

  1. Linux 文件管理(C语言库函数一)

    系统调用函数能够直接操作系统设备,C语言库函数是对系统调用函数的封装,增加了可移植性, C语言库函数可以在各个系统上运行,而系统调用则会因为系统不同而有一定的差别. 在读写文件这个操作上,系统函数每次 ...

  2. 集合映射Set(使用xml文件)

    如果持久类具有Set对象,可以在映射文件中使用set元素映射Set集合. set元素不需要索引元素. List和Set之间的区别是: Set只存储唯一的值. 我们来看看我们如何在映射文件中实现集合: ...

  3. postgresql学习文档

    字符串函数: http://www.php100.com/manual/PostgreSQL8/functions-string.html http://gavin-chen.iteye.com/bl ...

  4. OpenCV学习笔记十八:opencv_flann模块

    一,简介: Fast Library for Approximate Nearest Neighbors (FLANN)算法库.

  5. python 推荐算法

    每个人都会有这样的经历:当你在电商网站购物时,你会看到天猫给你弹出的“和你买了同样物品的人还买了XXX”的信息:当你在SNS社交网站闲逛时,也会看到弹出的“你可能认识XXX“的信息:你在微博添加关注人 ...

  6. lumen 单元测试

    laravel学院:http://laravelacademy.org/post/238.html 简书:https://www.jianshu.com/p/d8b3ac2c4623 问题解决:htt ...

  7. MySQL中enum类型数据,要传入字符串

    问题来源:公司业务,某张表中一个字段定义为: enum('0','1','2','3','4','5','6','7','8','9','10') NOT NULL DEFAULT '0' 某天跑脚本 ...

  8. SQL.Cookbook 读书笔记5 元数据查询

    第五章 元数据查询 查询数据库本身信息 表结构 索引等 5.1 查询test库下的所有表信息 MYSQL SELECT * from information_schema.`TABLES` WHERE ...

  9. jsx编译器 atom

    开始学习react es6 觉得没有合适的编译器.于是找到了个Atom. 官网 https://atom.io/ 下载安装. 双击运行即可完成安装. 安装后点击 file>setting> ...

  10. ASSERT(m_hWnd == hWndOrig) - 解决之

    该问题产生的原因为:创建类型为regular dll 的MFC dll中的窗口创建与其它DLL或EXE中的窗口创建混乱. 比如:dll1 中函数 fun1() 调用 dll2 中函数 fun2(),在 ...