You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given: S: "barfoothefoobarman" L: ["foo", "bar"] You should return the indices: [0,9].
class Solution { public: vector<int> findSubstring(string S, vector<string> &L) { // Start typing your C/C++ solution below // DO NOT write int main() function map<string, int> words; map<string, int> count; vector<int> res; if(L.size() == 0 || S.length() < L[0].size()*L.size() ) return res; for(int i = 0; i< L.size(); ++i){ ++words[L[i]]; } for(int i= 0; i <= S.length()- L[0].size()*L.size(); ++i) { count.clear(); int j; for(j = 0; j <L.size();++j){ string str = S.substr(i+j*L[0].size(), L[0].size()); if(words.find(str) == words.end()) break; count[str]++; if(count[str] > words[str]) break; } if(j == L.size()) res.push_back(i); } return res; } };