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  
leetcode 567  滑动窗口 leetcode 567  滑动窗口 leetcode 567  滑动窗口 leetcode 567  滑动窗口   TRANSLATE with leetcode 567  滑动窗口 COPY THE URL BELOW leetcode 567  滑动窗口 leetcode 567  滑动窗口 Back EMBED THE SNIPPET BELOW IN YOUR SITE leetcode 567  滑动窗口 Enable collaborative features and customize widget: Bing Webmaster Portal Back
上一篇:2020 ICPC 上海站(gym 102900) 题解


下一篇:操作系统中的『银行家算法(避免死锁)』