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.
For example, given:
S"barfoothefoobarman"
Words["foo", "bar"]
You should return the indices: [0,9]
.
(Order does not matter).
Give a string s and a list of words that have the same length. Find the starting position of all the substrings, the substring contains each word in the word list, and only once, the word cannot overlap in the substring.
That is to say, substring is one of the permutations of these words, if every starting point to do a check then need to compare n*m*k times, n is the length of S, M is the length of the word, K is the number of words. However, because the length of the word is the same, you can slice s, every m characters for a paragraph, there will be a total of M-type segmentation method, the starting slice location is 0~m-1. This is equivalent to the question: In a string, a substring containing a collection of words.
The time complexity is improved to O (N*m), and the code given is as follows:
vector<int> findsubstring (stringS, vector<string>&words) {Map<string,int>dict; for(intI=0; I<words.size (); ++i)++Dict[words[i]]; Vector<int>ret; intm =words.size (); intLen = words[0].size (); intn =s.size (); if(0==m)returnret; if(N <m)returnret; for(intI=0; i<len; ++i) {intCNT =0; Map<string,int>curdict; intJ=i, k=i;//J is the starting position, K is the end position while(k+len<=s.size ()) { stringstr =S.substr (k, Len); K+=Len; if(Dict.end () = =dict.find (str)) {CNT=0; Curdict.clear (); J=K; } Else if(++curdict[str] >Dict[str]) { --CURDICT[STR]; while(S.SUBSTR (j,len)! =str) { --curdict[s.substr (J,len)]; --CNT; J+=Len; } J+=Len; } Else { ++CNT; if(CNT = =m) {ret.push_back (j); --curdict[s.substr (J,len)]; --CNT; J+=Len; } } } } returnret;}
Substring with concatenation of all Words