【leetcode】438. Find All Anagrams in a String

problem

438. Find All Anagrams in a String

solution1:

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        if(s.empty()) return {};
        vector<int> res, pv(256, 0);
        for(auto a:p) pv[a]++;
        int sn = s.size();
        int i = 0;
        while(i<sn)
        {
            vector<int> tmp = pv;
            bool is = true;
            for(int j=i; j<i+p.size(); j++)
            {
                if(--tmp[s[j]]<0)
                {
                    is = false;
                    break;
                }
            }
            if(is) res.push_back(i);
            i++;
        }
        return res;
    }
};
View Code

参考

1. Leetcode_438. Find All Anagrams in a String;

猜你喜欢

转载自www.cnblogs.com/happyamyhope/p/10487588.html