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.

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

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

```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

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

2. 【leetcode】963. Minimum Area Rectangle II

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

3. 刷题76. Minimum Window Substring

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

4. LeetCode解题报告—— Minimum Window Substring &amp;&amp; Largest Rectangle in Histogram

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

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

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

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

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

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. 76. Minimum Window Substring

题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

随机推荐

1. .NET Framework 基础知识总结

C#: 1. public:同一个程序集的任何代码或引用该程序集的其他程序集都可以访问该类型或成员 internal:同一个程序集的任何代码都可以访问该类型或成员 private :只有在结构或类中的 ...

2. Hyper snap

图像->分辨率,设置成300dpi,一般论文的分辨率要求.

3. clearTimeout(timeoutfunc) 是否有必要执行

当使用 setTimeout() 方法的时候,是否必须执行 clearTimeout() ? 在 setTimeout() 内的函数执行之前,如果想要阻止执行该方法,是有必要执行 cleartTime ...

4. Unity3D内存释放

Unity3D内存释放 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. 这里写下关于Unity3D对于内存的管理与优化. Unity3D 里有两种动态加载机制:一个是Resourc ...

5. JAVA8 HashMap 新特性

1. 链表解决冲突的方式:   java中处理Hash散列后的冲突使用的是链表法:     java8之前只是使用的简单Entry链表存储键值对.java8后,在Entry队列的长度大于8之后,会自动 ...

6. JavaScript 框架------------AngularJS（上）

一.简单了解一下AngularJS AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. AngularJS 通过 指令 扩展了 ...