其他分享
首页 > 其他分享> > leetcode 567 滑动窗口

leetcode 567 滑动窗口

作者:互联网

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        unordered_map<char,int> need,window;
        
        for(char i: s1)
        {
            need[i]++;
        }
        int left=0;
        int right=0;
        int valid=0;
        
        while(right<s2.size())
        {

            char c = s2[right];
            right++;

            if(need.count(c))
            {
                window[c]++;
                if(window[c]==need[c])
                {
                    valid++;
                }
            }

            while(right-left>=s1.size())
            {
                if(valid==need.size())
                {
                    return true;
                }
                char l=s2[left];
                left++;
                if(need.count(l))
                {
                    if(window[l]==need[l])
                    {
                        valid--;
                    }
                    window[l]--;
                }
            }
        }
        return false;
    }
};

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

标签:int,s1,567,window,location,need,滑动,true,leetcode
来源: https://www.cnblogs.com/libin123/p/15175880.html