Minimum Window Substring

2020-12-13 02:08

阅读:499

soscw.com,搜素材
class Solution {
private:
    int hashs[256];
    int hasht[256];
    inline bool check()
    {
        for(int i=0;i256;i++)
            if(hashs[i]return false;
        return true;
    }
public:
    string minWindow(string S, string T) 
    {        
        for(int i=0;i256;i++)
        {
            hashs[i]=0;                
            hasht[i]=0;
        }

        for(int i=0;i            hasht[T[i]]++;
                    
        int minl=-1;
        int minr=S.length();
                
        for(int i=0;i            hashs[S[i]]++;

        if(!check()) return "";
        
        int l=0;
        int r=0;

        for(int i=0;i256;i++) hashs[i]=0;
        hashs[S[0]]=1;
        while(true)
        {
            if(check())
            {
                if(r-l                {
                    minl=l;
                    minr=r;
                    if(minr-minl+1==T.length()) break;
                }
                
                hashs[S[l]]--;
                l++;
            }
            else
            {
                r++;
                if(r==S.length()) break;
                hashs[S[r]]++;
            }
        }
        if(minl==-1return "";
        else return S.substr(minl,minr-minl+1);
    }
}; 

soscw.com,搜素材


评论


亲,登录后才可以留言!