Given X number of search terms, write an algorithm that will return the smallest substring from an article that contains all of the search terms.
Sigiloso
2 Rahul: it looks like the longest substring to me. One solution could be as follows: 1. Go through the text and create a list of indices for each term. Here the choice of a string search algorithm is important. For multiple patterns in the same text, the Rabin-Karp algorithm might be a reasonable choice, because it finds all k terms in O(n+k) time. 2. Go through all m instances/indices of all terms. At every index (belongs to one of the terms), find the closest instances of all other terms (prev or next index in their lists), thus obtaining the length of a substring with all terms and storing its minimum. This can be done in O(m k). Not sure, but maybe p.2 can be combined with p.1 or improved or eliminated. Another concern is whether it's one-time task or not, meaning if you constantly have to solve this for numerous sets of terms, then completely another (and probably more complicated) solution might be faster - something with text pre-processing, likely involving suffix trees/arrays, LCP arrays, etc.