s4553711
7/26/2017 - 3:02 PM

438.cpp

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        if (s.empty()) return {};
        vector<int> res, cnt(128, 0);
        int ns = s.size(), np = p.size(), i = 0;
        for (char c : p) ++cnt[c];
        while (i < ns) {
            bool success = true;
            vector<int> tmp = cnt;
            for(int j = i; j < i + np; j++) {
                //cout << j << " to " << i + np << "<> " << s[j] << " : " << tmp[s[j]] << endl;
                if (--tmp[s[j]] < 0) {
                    success = false;
                    break;
                }
            }
            if (success) res.push_back(i);
            i++;
        }
        return res;
    }
};