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]
.
假设L中的单位长度为n,依次从S中取长度为n的子串,如果在L中,就记下来。如果在L中,但是已经匹配过了,说明匹配重复,也需要从S的下一个位置开始重新匹配,因为匹配要是连续的,中间不允许插入其他不匹配或者重复匹配的单词。需要借助hash或map,如果整个L都匹配完了,就算是一个concatenation;当匹配错误的时候,S右移一个位置。
C++实现代码:
#include#include #include #include