【LeetCode】76. Minimum Window Substring
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的更多相关文章
- 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...
- 【一天一道LeetCode】#76. Minimum Window Substring
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 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 ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- [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 ...
- [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 ...
随机推荐
- [转]web服务器apache架构与原理 &apache 监控
web服务器 在开始了解Apache前,我 ...
- 阻止picker.js插件弹出键盘
<input id="focus" type="text" value="" placeholder="手机号" ...
- java多线程知识点汇总(四)多线程知识点脉络图
1.多线程安全问题 1)synchronized关键字:如何加锁的问题,选择synchronized方法还是synchnized代码块. 选择哪个锁问题,this对象,还是class对象(针对stat ...
- java并发集合知识点(二)
我们平时写程序需要经常用到集合类,比如ArrayList.HashMap等,但是这些集合不能够实现并发运行机制,这样在服务器上运行时就会非常的消耗资源和浪费时间,并且对这些集合进行迭代的过程中不能进行 ...
- 【资料】wod食物
注意:1. 除非另外注明, 所有效果持续时间为整个地城2. 某几样食物若使用午饭时间技能, 效果只有LV1 (lunch level -25), 请小心服用. X技能等级 = 技能等级 焖豆属性奖励体 ...
- 如何将Emmet(ZenCoding)安装到到Dreamweaver8?
用过其他版本的Dreamweaver,还是习惯了Dreamweaver8,占用少,插件也容易安装,下面讲的是ZenCoding插件的安装方法,当然现在这个已经叫Emmet了. 安装方法: a.下载dw ...
- unix简史及应用
Unix 简史 1965年时,贝尔实验室(Bell Labs)加入一项由奇异电子(General Electric)和麻省理工学院(MIT)合作的计画:该计画要建立一套多使用者.多任务.多层次(mul ...
- .NET:用T4消除代码重复,对了,也错了
背景 我需要为int.long.float等这些数值类型写一些扩展方法,但是我发现他们不是一个继承体系,我的第一个思维就是需要为每个类型重复写一遍扩展方法,这让我觉得非常不爽,但是我还是不情愿的写了, ...
- MNI模板和Talairach 模板的对比
The MNI brain and the Talairach atlas SPM 96 and later use standard brains from the Montreal Neurolo ...
- gpu和cpu区别
GPU的功耗远远超过CPUCache, local memory: CPU > GPU Threads(线程数): GPU > CPURegisters: GPU > CPU 多寄存 ...