给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 “” 。
注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。
示例 1:
输入:s = “ADOBECODEBANC”, t = “ABC”
输出:“BANC”
示例 2:
输入:s = “a”, t = “a”
输出:“a”
提示:
1 <= s.length, t.length <= 105
s 和 t 由英文字母组成
class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char,int> windows;
unordered_map<char,int> needs;
for(auto c:t) needs[c]++;
int match = 0;
int left = 0, right = 0;
int start =0, len = INT_MAX;
while(right<s.size()){
char c1 = s[right];
windows[c1]++;
right++;
//c1符合
if(needs.count(c1)){
if(windows[c1] == needs[c1]) match++;
}
while(match==needs.size()){
if(right-left<len){
len = right-left;
start = left;
}
char c2 = s[left];
windows[c2]--;
left++;
if(needs.count(c2)){
if(windows[c2] < needs[c2])
match--;
}
}
}
return len == INT_MAX?"":s.substr(start,len);
}
};