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. MySQL同步状态双Yes的假象及 seconds_behind_master的含义

    MySQL同步状态双Yes的假象及seconds_behind_master的含义   近期由于特殊原因有一台主库宕机了一个小时没有处理,说起来这是个挺不好啥意思的事情,但是由于这个事情反而发现个比较 ...

  2. C++标准库之tuple

    构造 构造函数 tuple的构造函数很普通,没啥说的. default (1) constexpr tuple();默认构造函数 copy / move (2) tuple (const tuple& ...

  3. .net 连接Redis

    Redis安装 公司业务需要,弄了个类似于消息队列的东西,由客户端一直轮询取得信息.之前一直是走表的,但是效率以对表的使用都太过频繁,想试着用一下Redis,看看效率怎么样. 首先是安装Redis,这 ...

  4. 10道mysql查询语句面试题

    1.https://www.yanxurui.cc/posts/mysql/10-sql-interview-questions/ 2.http://mm.fancymore.com/reading/ ...

  5. Linux之(node.js)服务

    1.1下载源码 你需要在下载最新的Nodejs版本, https://nodejs.org/en/download/ http://nodejs.org/dist/ 现在以node-v7.7.1.ta ...

  6. Android无线测试之—UiAutomator UiObject API介绍二

    点击与长按 一.组件区域位置关系 Rect 对象代表一个矩形区域 [Left,Top] [Right,Bottom] 二.点击与长按API 返回值 API 描述 boolean click() 点击对 ...

  7. ie10 css hack 条件注释等兼容方式整理

    点评:ie10已经上线一段时间了,相信已经有一部分前端潮人体验过了,截至到现在,在ie6到ie9的浏览器各种各样的古怪行为,开发人员不得不使用条件注释,有条件的类,和其他特定于IE的css hack来 ...

  8. POJ1182食物链(并查集经典好题)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66964#problem/E 题目思路:主要有两种思路:1.带权并查集2.挑战程 ...

  9. Android输入法的显示与隐藏

    显示输入法: public void ShowSoftInput(View v) { // v 接受输入的控件 mInputMethodManager = (InputMethodManager) ( ...

  10. Java源码之String

    本文出自:http://blog.csdn.net/dt235201314/article/details/78330377 一丶概述 还记得那会的“Hello World”,第一个程序,输出的Str ...