You is given a string, s, and a list of words, words, that is all of the same length. Find all starting indices of substring (s) in S that's a concatenation of each word in words exactly once and without any Intervening characters.
Example 1:
Input: s = "Barfoothefoobarman", [0,9]
explanation:substrings starting at index 0 and 9 is "Barfoor" and "Foobar" respectively. The output order does not matter, returning [9,0] is fine too.
Example 2:
Input: s = "Wordgoodstudentgoodword", []
AC Code:
class solution {public:vector<int> findsubstring (string s, vector< string>& words) {unordered_map<string, int> Mario; Vector<int> res={}; int len1 = s.length (), num = Words.size (); if (len1 = = 0 | | num = 0) return res; int len2 = Words[0].length (); for (int i = 0; i < words.size (); ++i) {mario[words[i]]++; } for (int i = 0; i < len1-num*len2+1; ++i) {unordered_map<string, int> seen; int j = 0; for (; j < num; ++j) {String word = S.substr (i+j*len2, len2); if (Mario.find (word)! = Mario.end ()) {seen[word]++; if (Seen[word] > Mario[word]) break; } else break; } if (j = = num) res.push_back (i); } return res; }};
Runtime: faster than 49.73% of C + + online submissions for Substring with concatenation of all Words.
Substring with concatenation of all Words