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.

由于大小写字母的ASCII码不大于128,因此开辟两个数组存储信息。

needFind数组存储T字符串每个字符出现次数。例如:needFind['A']=5意为T中A出现5次。

Found数组存储S字符串在[begin,end]窗口内每个字符出现次数。

算法核心思想如下:

在保证[begin,end]窗口包含T中所有字符的条件下,延伸end,收缩begin。

进行一次扫描后,记录符合条件的最小窗口(end-begin+1)表示的字符串。

有个问题:怎样才知道[begin,end]窗口包含了T中所有字符?

我使用count记录剩余“有效”字符数,当count达到0时,即可说明[begin,end]包含了T。

注意:“有效”的意思是指,当前延伸得到的S[end]字符,使得[begin,end]更进一步包含T,而不是重复劳动。

比如说,T="a", [begin,end]已经包含"a",再延伸得到"aa",只是无效操作,并没有使得[begin,end]更接近T,有效字符数仍为1.

class Solution {
public:
string minWindow(string S, string T) {
int begin = ;
int end = ;
int minbegin = ;
int minend = ;
int minSize = INT_MAX;
vector<int> needFind(, );
vector<int> Found(, );
for(int i = ; i < T.size(); i ++)
needFind[T[i]] ++;
Found[S[]] ++;
int count = T.size();
if(needFind[S[]] >= Found[S[]])
count --;
while(true)
{
if(count == )
{//shrink begin
while(Found[S[begin]] > needFind[S[begin]])
{
Found[S[begin]] --;
begin ++;
}
int size = end-begin+;
if(size < minSize)
{
minbegin = begin;
minend = end;
minSize = size;
}
}
if(end < S.size())
{
end ++;
Found[S[end]] ++;
if(needFind[S[end]] >= Found[S[end]])
count --;
}
else
break;
}
if(minSize != INT_MAX)
return S.substr(minbegin, minSize);
else
return "";
}
};

【LeetCode】76. Minimum Window Substring的更多相关文章

  1. 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)

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

  2. 【一天一道LeetCode】#76. Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  4. 刷题76. Minimum Window Substring

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

  5. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  6. LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram

    1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...

  7. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

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

  9. [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 ...

随机推荐

  1. Codeforces Round #302 (Div. 2) A. Set of Strings 水题

    A. Set of Strings Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/pr ...

  2. git用法资料

    上网看到一篇不错的GIT教程,与大家共享(图片上传实在太麻烦),请见具体地址: http://www.liaoxuefeng.com/wiki/0013739516305929606dd1836124 ...

  3. debian8上安装pyspider - pyspider中文文档 - pyspider中文网

    debian8上安装pyspider - pyspider中文文档 - pyspider中文网   #apt-get install python python-dev python-distribu ...

  4. Selenium2+python自动化70-unittest之跳过用例(skip)

    前言 当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例 ...

  5. sessionid与cookie

    转自:http://smiky.iteye.com/blog/649164 发现自己真的是很笨,过去一直用jsp,从来不用怕心用户信息放在session里面会找不到,现在不用jsp,前台全用html, ...

  6. dwz关闭当前dialog

    首先,前台代码如下: <form method="post" class="pageForm required-validate" onsubmit=&q ...

  7. jquery微博实例

    1.代码实例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  8. [Android Pro] Android系统手机端抓包方法 和 通过File查看应用程序流量

    adb shellcat proc/uid_stat/%uid%/tcp_snd  proc/uid_stat/%uid%/tcp_rcv ------------------------------ ...

  9. [Linux] git send-email的使用

    1. git send-email is included in an individual package, named "git-email":$ sudo apt-get i ...

  10. HackRF采集调频广播基带数据并使用Ocatve解调播放

    使用hackrf_transfer工具在Linux系统上,采集当地的一个调频广播,使用的采样频率为8MHz.得到IQ交错存储的8位有符号基带数据,在Octave中,先进行50倍抽取,变换到160K采样 ...