【leetcode】438. Find All Anagrams in a String

problem

438. Find All Anagrams in a String

solution1:

【leetcode】438. Find All Anagrams in a String
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;

上一篇:c++引擎开发


下一篇:Python:Spark:当key不是行中的第一个键时,Dataframe.subtract返回所有内容