class Solution { public: bool findAll(vector<int> to_find,vector<int> has_find){ bool flag=true; for(int i=0;i<256;i++){ if(to_find[T[i]]<=has_find[T[i]]){ flag=flag&&true; } else{ flag=false; break; } } return flag; } string minWindow(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function int lenT=T.length(); int lenS=S.length(); if(lenT==0) return ""; if(lenS==0) return ""; vector<int> to_find(256); vector<int> has_find(256); for(int i=0;i<lenT;i++){ to_find[T[i]]++; } int left=0; int minleft=0; int minLength=INT_MAX; for(int i=0;i<lenS;i++){ if(to_find[S[i]]>0){ has_find[S[i]]++; //cout<<S[i]; if(findAll(to_find,has_find)){//window contains all chars in T,then move left //assert(S[i]=='b'); //cout<<"in"<<endl; //cout<<S[i]<<endl; while(true){//move left //cout<<has_find[S[left]]<<endl; //cout<<to_find[S[left]]<<endl; if(has_find[S[left]]<=to_find[S[left]]&&has_find[S[left]]!=0){//condition is left is redundant break; } //cout<<"in"<<endl; if(has_find[S[left]]!=0) has_find[S[left]]--;// left++; } if(minLength>(i-left+1)){//see if minlength minLength=i-left+1; minleft=left; } //has_find[S[left]]--; //left++; //cout<<S[left]<<endl; } } } if(minLength==INT_MAX){ return ""; } else{ //cout<<minleft<<minLength; return S.substr(minleft,minLength); } } };
leetcode minimum window substring
最新推荐文章于 2017-09-07 21:24:22 发布