【中等】字符串的排列

【中等】字符串的排列_第1张图片

class Solution {
private:
    unordered_map<char, int> need, window;
public:
    bool checkInclusion(string s1, string s2) {
        for (char c : s1) need[c]++;
        int left = 0, right = 0, valid = 0, len = INT_MAX;
        while (right < s2.size())
        {
            char c = s2[right++];
            if (need.count(c))
            {
                window[c]++;
                if (window[c] == need[c])
                    valid++;
            }
            while (valid == need.size())
            {
                if (right - left < len)
                    len = right - left;
                if (len == s1.size())
                    return true;
                char d = s2[left++];
                if (need.count(d))
                {
                    if (window[d] == need[d])
                        valid--;
                    window[d]--;
                }
            }
        }
        return false;
    }
};

你可能感兴趣的:(#,滑动窗口,leetcode,算法,职场和发展)