[RainbowSecret] Naive C++ implementation


  • 0
    class Solution {
    public:
        vector<int> findSubstring(string s, vector<string>& words) {
            int len=s.size();
            int len_word=words[0].size(), count=words.size();
            map<string, int> dict;
            vector<int> result;
            for(auto word:words)  dict[word]++;
            for(int i=0; i<=len-len_word*count; i++){
                map<string, int> found;
                int j=0;
                for(; j<count; j++){
                    string temp=s.substr(i+j*len_word, len_word);
                    if(dict.find(temp)==dict.end())
                        break;
                    else{
                        found[temp]++;
                        if(found[temp]>dict[temp])  break;
                    }
                }
                if(j==count)    result.push_back(i);
            }
            return result;
        }
    };

Log in to reply
 

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