Very simple, brute force 500ms c++ solution.


  • 0
    S
    class Solution {
    public:
    
    vector<int> findSubstring(string s, vector<string>& words) {
    	unordered_map<string, int> table;
    	int size = 0;
    	vector<int> result;
    	int len = words[0].size();
    	int word_size = words.size();
    	for (auto word : words) {
    		table[word] += 1;
    	}
    
        int word,j;
        unordered_map<string, int> status;
    	for (int i = 0; i <s.length()-((len*word_size)-1) ;i+=1) {
    		status = table;
    		word = 0;
    		j = i;
    		while (--status[s.substr(j, len)] >= 0 && word <= word_size){
    			++word;
    			j += len;
    		}
    		if (word >= word_size)
    			result.push_back(j - ((word_size*len)));
    	}
    	return result;
    }
    };

Log in to reply
 

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