Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> indices;
if(s.size() == 0 || words.size() == 0)
return indices;
unordered_map<string, int> counts;
for(string word: words){
counts[word]++;
}
int len_s = s.length(), numW = words.size(), len_w = words[0].size();
for(int i = 0; i< len_s - numW*len_w + 1; i++){
unordered_map<string, int> detect;
int j = 0;
for(; j < numW; j++){
string tempW = s.substr(i + j*len_w, len_w);
if(counts.find(tempW)!=counts.end()){
detect[tempW]++;
if(detect[tempW]>counts[tempW])
break;
}
else
break;
}
if(j == numW) indices.push_back(i);
}
return indices;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment