I asked to solve the following problem in a programming contest (facebook recruitment)
Input: list of substrings:
{"bar","foo","hi"} //from 1 to 100 sub-strings
and suggestion:
"hellothisisfoobarhi" //from 1 to 1000000 character
Output: 12 // index of the first match in the sentence (foo)
another example:
substring: {"hello", "hello"}
offer: "hiJonIamSayinghihiforYou"
output: 15 // index hi, the second "hi", because the first "hi" is just a trick, sub-sentece is "hi" hi "
one more ex:
substrings: {"hello", "Foo"}
offer: "sayfoohi"
conclusion: 6 // the order does not matter, they just have to be next to each other
Who knows a good algorithm for this problem?
source share