每日一题ing,今天是个hard题76. Minimum Window Substring
class Solution {
public:
string minWindow(string s, string t) {
vector <int> num(128,0);
vector <bool> flag(128,false);
for(int i=0;i<t.length();i++){
flag[t[i]]=true;
num[t[i]]++;
}
bool fflag=true;
int l=0,min_l=0,min_size=s.length()+1,cnt=0;
for(int i=0;i<s.length();i++){
if(flag[s[i]]){
fflag=false;
num[s[i]]--;
if(num[s[i]]>=0){
cnt++;
}
while(cnt==t.length()){
if(i-l+1<min_size){
min_size=i-l+1;
min_l=l;
}
if(flag[s[l]]&&++num[s[l]]>0){
cnt--;
}
l++;
}
}
}
if(min_size<t.length()||min_size>s.length()||fflag){
return "";
}
return s.substr(min_l,min_size);
}
};
|