Problem
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:
(order does not matter).
[0,9].(order does not matter).
Algorithm
Use a hash table to count the occurrence of strings in list L. Scan S from left to right. Start from any index in S, if the substring S.substring(index, index + L.length()) is a concatenation of words in L, we are done.
This problem is smilier to Longest Substring without Repeating Characters, which maintained a window and using a hash table to count the occurrence of characters.
No comments:
Post a Comment