https://leetcode-cn.com/problems/minimum-window-substring/
思路:滑動窗口,搞兩個指針,把右移直到滿足題意,然後再把右移直到不滿足題意,這時記錄一下最小值,重複這個過程即可。
class Solution {
public:
int viss[256];
int vist[256];
string minWindow(string s, string t) {
int siz=t.size(),ct=0;
for(int i=0;i<siz;i++)
{
if(!vist[t[i]])
++ct;
++vist[t[i]];
}
siz=s.size();
int l=0,r=0;
string ans;
while(r<siz&&ct)
{
++viss[s[r]];
if(vist[s[r]]&&viss[s[r]]==vist[s[r]])
--ct;
++r;
}
if(ct)
return ans;
int al=l,ar=r-1;
while(1)
{
while(!vist[s[l]]||--viss[s[l]]>=vist[s[l]])
++l;
if(r-l<ar-al+1)
ar=r-1,al=l;
while(r<siz&&viss[s[l]]<vist[s[l]])
++viss[s[r++]];
if(viss[s[l]]<vist[s[l]])
break;
++l;
}
ans=s.substr(al,ar-al+1);
return ans;
}
};