题目:
给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字母的最小子串。
示例:
输入: S = "ADOBECODEBANC", T = "ABC"
输出: "BANC"
说明:
如果 S 中不存这样的子串,则返回空字符串 ""。
如果 S 中存在这样的子串,我们保证它是唯一的答案。
思路:
代码:
class Solution {
public:
int INI_MAX = int(pow(2.0, 31.0)) - 1;
string minWindow(string s, string t) {
//记录最短子串的开始位置和长度
int start = 0, minLen = INI_MAX;
int left = 0,right = 0;
unordered_map<char, int>window;
unordered_map<char, int>needs;
for (char c : t) needs[c]++;
int match = 0;// 记录 window 中已经有多少字符符合要求了
while (right<s.size())
{
char c1 = s[right];
if (needs.count(c1)) {
window[c1]++;
if (window[c1] == needs[c1])
match++;
}
right++;
while (match==needs.size())
{
if (right - left < minLen)
{
start = left;//更新最小子串的位置和长度
minLen = right - left;
}
char c2 = s[left];
if (needs.count(c2)) {
window[c2]--;
if (window[c2] < needs[c2])
match--;
}
left++;
}
}
return minLen == INI_MAX ? "" : s.substr(start, minLen);
}
};