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. http://www.tangible-engineering.com/tangible_t4editor.html

    T4 Template Tutorials   http://www.tangible-engineering.com/tangible_t4editor.html

  2. PHP大转盘中奖概率算法实例

    本文实例讲述了PHP大转盘中奖概率算法的实现方法,分享给大家供大家参考.具体如下: 大转盘是最近很多线上网动中一个比较有意思的东西了,下面我们就来看看这个大转盘中奖概率算法与例子,希望对各位有所帮助. ...

  3. Object-C编程基础总结:

    1,nil,NULL,NSNull:nil用来给对象附值,object—c里允许对象为空,空对象也可以接受消息.但是不允许指针为空,NULL是给任何指针附值的.所以NULL只在C或C++里才用.NSN ...

  4. gdb参考手册

    http://www.sourceware.org/gdb/current/onlinedocs/gdb.html

  5. 百度王一男: DevOps 的前提是拆掉业务-开发-测试-运维中间的三面墙

    这是一个创建于 375 天前的主题,其中的信息可能已经有所发展或是发生改变. 由数人云.优维科技.中生代社区联合发起的 系列 Meetup < DevOps&SRE 超越传统运维之道&g ...

  6. Mysql配置文件my.ini详解

    以下是Mysql数据库服务器配置文件my.ini的详细配置.应用场合是InnoDB引擎,2核CPU, 32位SUSE. [client] #password = your_password port  ...

  7. word2010图片仅仅显示边框

    有两个可能的原因: 1.图片所在段落的行间距被设置成固定值了 解决:选择所在段落,右键选择段落--弹出[段落]设置对话框--把固定行距改为"单倍行距"或其它--确定. 2.显示设置 ...

  8. .NET:遇到并发问题,什么样的情况下需要自动重试?

    背景 多用户系统会出现并发问题,应对并发问题的两种方式是“悲观锁”和“乐观锁”,多数情况下都会采用“乐观锁”,这引发了一个问题,如果检查出乐观并发异常如何重试?是让最终用户手工重试?还是让系统自动重试 ...

  9. 解决spark程序报错:Caused by: java.util.concurrent.TimeoutException: Futures timed out after [300 seconds]

    报错信息: 09-05-2017 09:58:44 CST xxxx_job_1494294485570174 INFO - at org.apache.spark.sql.catalyst.erro ...

  10. [置顶] JDK工具(一)–Java编译器javac

    1.概述    javac.exe: Java编译器,将Java源代码转换成字节码. 2.用法    javac <选项> <源文件> (使用过程中发现,javac <源 ...