Pergunta de entrevista da empresa Google

Given a search terms, find the minimum window containing all the words

Respostas da entrevista

Sigiloso

19 de abr. de 2011

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).

1

Sigiloso

28 de ago. de 2010

is it Dyn Programming's Least Common subsequence problem??

1

Sigiloso

17 de ago. de 2010

Had a general idea, but could not elaborate the answer. I knew it can be done in linear time, but could not get to code it.