Posted on

Description

Submission

class Solution {

    bool isSame(map<char, int>& m1, map<char, int>& m2) {
        for(auto& p: m1) {
            if(p.second != m2[p.first]) return false;
        }
        return true;
    }

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

        map<char, int> s1Freq, s2Freq;
        int j = 0;
        for(; j < s1.size(); ++j) {
            s1Freq[s1[j]]++;
            s2Freq[s2[j]]++;
        }
        s2Freq[s2[--j]]--;
        for(int i = 0; j < s2.size(); ++i, ++j) {
            s2Freq[s2[j]]++;
            if(isSame(s1Freq, s2Freq)) return true;
            s2Freq[s2[i]]--;
        }

        return false;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *