Posted on

Description

Submission

class Solution {
    int pCount[26];
    int sCount[26];

    inline bool isSame() {
        for(int k = 0; k < 26; ++k) {
            if(pCount[k] != sCount[k]) return false;
        }
        return true;
    }
public:
    vector<int> findAnagrams(string s, string p) {
        int m = s.length();
        int n = p.length();

        for(char ch: p) {
            ++pCount[ch-'a'];
        }

        vector<int> rets;
        for(int i = 0, j = 0; i <= m - n; ++i) {
            while(j - i < n) {
                ++sCount[s[j++]-'a'];
            }

            if(isSame()) {
                rets.push_back(i);
            }

            --sCount[s[i]-'a'];
        }
        return rets;
    }
};

Leave a Reply

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