Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
722 B
1
Indexable
Never
class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        int m=words[0].size();
        int n=words.size(); //n words size of each m
        vector<int>res;
        if(s.size() <m*n) return res;
        unordered_map<string,int>freq;
        for(auto i:words) freq[i]++;

        for(int i=0;i<=s.size()-n*m;i++){
            unordered_map<string,int>mp(freq);
            int t;
            for(t=0;t<n;t++){
                string wc=s.substr(i+t*m,m);
                if(mp.find(wc)!=mp.end()){
                    if(--mp[wc] ==-1) break;
                } 
                else break;
            }
            if(t==n) res.push_back(i);
        }
        return res;
    }
};
Leave a Comment