Leetcode 567 Solution

This article provides solution to leetcode question 567 (permutation-in-string)

https://leetcode.com/problems/permutation-in-string

Solution

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        if (s1.size() > s2.size())
            return false;

        map<char, int> m;
        map<char, int> t;
        for (char ch : s1)
            t[ch]++;

        int l = 0;
        int r = 0;

        while (r < s2.size())
        {
            char ch = s2[r];
            m[ch]++;

            while (m[ch] > t[ch])
                m[s2[l++]]--;

            r++;

            if (r - l == s1.size())
                return true;
        }

        return false;
    }
};