[76. 最小覆盖子串](滑动窗口双指针+哈希表)(LeetCode)
可以使用滑动窗口和双指针需要满足单调性的条件,枚举字符串重点位置i可知i往后走的话,j一定不会往前走

class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char, int> hs, ht;
for(auto c : t) ht[c]++;
string res = "";
int cnt = 0;
for(int i = 0, j = 0; i < s.size(); ++i){
hs[s[i]]++;
if(hs[s[i]] <= ht[s[i]]) ++cnt;
while(hs[s[j]] > ht[s[j]]) hs[s[j++]]--;
if(cnt == t.size()){
if(res.empty() || i - j + 1 < res.size()){
res = s.substr(j, i - j + 1);
}
}
}
return res;
}
};

浙公网安备 33010602011771号