LeetCode(Minimum Window Substring)
string minWindow(string s,string t)
{
int m=s.size(),n=t.size();
if(m==0)return "";
if(m<n)return "";
vector<int> expected(256,0),appeared(256,0);
for(int i=0;i<n;++i)++expected[t[i]];
//i指向窗口的开头,j指向窗口的结尾
int i=0,cnt=0,minLength=INT_MAX,minIndex=0;
for(int j=0;j<m;++j)
{
if(expected[s[j]]>0)
{
++appeared[s[j]];
if(appeared[s[j]]<=expected[s[j]])++cnt;
}
//找到一个包含t中所有字符的窗口
if(cnt==n)
{
//i右移
while(expected[s[i]]==0||appeared[s[i]]>expected[s[i]])
{
--appeared[s[i]];
++i;
}
if(j-i+1<minLength)
{
minLength=j-i+1;
minIndex=i;
}
}
}
if(cnt<n)return "";
else return s.substr(minIndex,minLength);
}