category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

438. Find All Anagrams in a String

class Solution {
    public:
    vector<int> findAnagrams(string s, string t) {
        if (s.size() < t.size()) return {};
        vector<int> ans, st(26, 0), must(26, 0);
        int k = t.size();

        for (int i = 0; i < k; i++)
            st[s[i] - 'a'] ++, must[t[i] - 'a'] ++;

        if (st == must)
            ans.push_back(0);

        for (int i = k; i < s.size(); i++) {
            st[s[i - k] - 'a'] --;
            st[s[i] - 'a'] ++;

            bool good = true;
            for (int i = 0; i < 26; i++) {
                if (st[i] != must[i]) {
                    good = false;
                    break;
                }
            }
            if (good)
                ans.push_back(i - k + 1);
        }
        return ans;
    }
};