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:
L:
S:
"barfoothefoobarman"L:
["foo", "bar"]
You should return the indices:
[0,9].
(order does not matter).
Solution:
Simple implementation, use map to store the number of occurance, to consider two or more happening case.
vector
vector
int num = L.size();
int len = L[0].size();
if (num==0){return res;}
map
for (int i=0;i
int i=0;
while ((i+num*len-1)
int j=0;
while (j
if (mp.find(subs)==mp.end()){
break;
}else{
mp2[subs]++;
if (mp2[subs]>mp[subs]){
break;
}
j++;
}
}
if (j==num){res.push_back(i);}
i++;
}
return res;
}
No comments:
Post a Comment