Given a search terms, find the minimum window containing all the words
Sigiloso
I think the following will work. Say there are three words w1, w2, w3 we care about which occur as part of the sequence of words a0, a1, ...., an among which there can be other words as well. Maintain three indices, i1, i2, i3. If word ak equals to w1, update i1 = k, and likewise if ak equals w2 or w3 then update i2 or i3 with value k. Don't update any of these three indices if word ak is none of the three words we are interested in. For every k then, the window having all these search terms will be max(i1, i2, i3) - min(i1, i2, i3) (assuming that each of the words w1, w2, w3 were already encountered at least once by now). So just keep track of the min of this expression when iterating through k. The algorithm is O(n).