艾恩凝
个人博客 https://aeneag.xyz/
公众号 技术乱舞
每日一练,保持手感
2021/10/18
疲乏的大晚上再刷一道题吧
题目
https://leetcode-cn.com/problems/minimum-window-substring/
分析
滑动窗口,左右都有标记
题解
class Solution {
public:
string minWindow(string s, string t) {
int cnt= 0 ,count = t.size();
unordered_map<char, int> tt,ss;
string res;
for(auto& c : t)++tt[c];
for(int i = 0 , j = 0 ; i < s.size() ; ++i){
++ss[s[i]];
if(ss[s[i]] <= tt[s[i]])++cnt;
while(ss[s[j]] > tt[s[j]])--ss[s[j++]];
if(cnt == count){
if(res.empty() || i-j+1 < res.size()){
res = s.substr(j,i-j+1);
}
}
}
return res;
}
};
欢迎关注 #公众号:技术乱舞 一起交流
灵魂碰撞