567. Permutation in String

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        unordered_map<char, int> dict;
        for (char c : s1) dict[c]++;
        int l = 0, r = 0, cnt = s1.size();
        while (r < s2.size()) {
            if (dict[s2[r]]) cnt--;
            dict[s2[r++]]--;
            if (!cnt) return true;
            if (r - l == s1.size()) {
                dict[s2[l]]++;
                if (dict[s2[l++]]) cnt++;
            }
        }
        return false;
    }
};

results matching ""

    No results matching ""