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

Example:

```Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
```

Note:

• If there is no such window in S that covers all characters in T, return the empty string `""`.
• If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

- 先扫描一遍T，把对应的字符及其出现的次数存到 HashMap 中。

- 然后开始遍历S，就把遍历到的字母对应的 HashMap 中的 value 减一，如果减1后仍大于等于0，cnt 自增1。

- 如果 cnt 等于T串长度时，开始循环，纪录一个字串并更新最小字串值。然后将子窗口的左边界向右移，如果某个移除掉的字母是T串中不可缺少的字母，那么 cnt 自减1，表示此时T串并没有完全匹配。

```class Solution {
public:
string minWindow(string s, string t) {
string res = "";
unordered_map<char, int> letterCnt;
int left = , cnt = , minLen = INT_MAX;
for (char c : t) ++letterCnt[c];
for (int i = ; i < s.size(); ++i) {
if (--letterCnt[s[i]] >= ) ++cnt;
while (cnt == t.size()) {
if (minLen > i - left + ) {
minLen = i - left + ;
res = s.substr(left, minLen);
}
if (++letterCnt[s[left]] > ) --cnt;
++left;
}
}
return res;
}
};```

```class Solution {
public:
string minWindow(string s, string t) {
vector<int> letterCnt(, );
int left = , cnt = , minLeft = -, minLen = INT_MAX;
for (char c : t) ++letterCnt[c];
for (int i = ; i < s.size(); ++i) {
if (--letterCnt[s[i]] >= ) ++cnt;
while (cnt == t.size()) {
if (minLen > i - left + ) {
minLen = i - left + ;
minLeft = left;
}
if (++letterCnt[s[left]] > ) --cnt;
++left;
}
}
return minLeft == - ? "" : s.substr(minLeft, minLen);
}
};```

Github 同步地址：

https://github.com/grandyang/leetcode/issues/76

Substring with Concatenation of All Words

Minimum Size Subarray Sum

Sliding Window Maximum

Permutation in String

Smallest Range

Minimum Window Subsequence

https://leetcode.com/problems/minimum-window-substring/

https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problems

LeetCode All in One 题目讲解汇总(持续更新中...)

## [LeetCode] Minimum Window Substring 最小窗口子串的更多相关文章

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

2. [Leetcode] minimum window substring 最小字符窗口

Given a string S and a string T, find the minimum window in S which will contain all the characters ...

3. [LeetCode] Minimum Window Subsequence 最小窗口序列

Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...

4. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

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

6. lintcode 中等题：minimum window substring 最小子串覆盖

题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

7. [LeetCode] 727. Minimum Window Subsequence 最小窗口序列

Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...

8. Leetcode 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]Minimum Window Substring @ Python

原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...

## 随机推荐

1. [译] 在Web API 2 中实现带JSON的Patch请求

原文链接:The Patch Verb in Web API 2 with JSON 我想在.NET4.6 Web API 2 项目中使用Patch更新一个大对象中的某个字断,这才意识到我以前都没有用 ...

2. PHP常用函数整理

推荐网址:http://php.net/manual/zh/http://www.w3cschool.cc/php/php-ref-array.html 错误报告: error_reporting(E ...

工作中遇到一个表,用来筛选信息. 但是表现在是excel,为了能在spark中使用,找了下解决办法 http://stackoverflow.com/questions/4125385/java-ho ...

4. CORBA IOR学习

Interoperable Object References: IOR IOR用于表示一个对象引用,我们知道,当我们在客户端一个CORBA对象的时候,接触的并不是真正的对象,而是这个对象的代理(Pr ...

5. Windows下使用Git和GitHub.com

1.首先介绍一下什么是Git和GitHub       Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.在推出后,Git在其它项目中也取得了很大 ...

6. Cheatsheet: 2014 02.01 ~ 02.28

Database Managing disk space in MongoDB When to use GridFS on MongoDB .NET The Past, Present, and Fu ...

7. BZOJ 3160 万径人踪灭 解题报告

这个题感觉很神呀.将 FFT 和 Manacher 有机结合在了一起. 首先我们不管那个 “不能连续” 的条件,那么我们就可以求出有多少对字母关于某一条直线对称,然后记 \$T_i\$ 为关于直线 \$i\$ ...

8. 给file_get_contents函数设置超时时间

\$opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60, ) ); \$context = str ...