AC BroutForce C++ , easy understanding


  • 1
    Z

    class Solution {

    public:

    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> res;
        if(words.empty()) return res;
        int wordLen = words[0].size();
        int minLen = words.size() * wordLen;
        if(s.size() < minLen) return res;
        unordered_map<string, int> table, originTable;
        for(string &word : words){
            ++originTable[word];
        }
        bool approve;
        for(int i = 0; i <= s.size() - minLen; ++i){
            table = originTable;
            approve = true;
            for(int j = 0; j < words.size(); ++j){
                string cur = s.substr(i + j*wordLen, wordLen);
                if(table.find(cur) == table.end() || table[cur] == 0){
                    approve = false;
                    break;
                }
                else{
                    table[cur] -= 1;
                }
            }
            if(approve) res.push_back(i);
        }
        return res;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.