Google search results: How to find the minimum window that contains all the search keywords?

后端 未结 5 1980
抹茶落季
抹茶落季 2020-12-12 21:10

What is the complexity of the algorithm is that is used to find the smallest snippet that contains all the search key words?

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 22:06

    This is an interesting question. To restate it more formally: Given a list L (the web page) of length n and a set S (the query) of size k, find the smallest sublist of L that contains all the elements of S.

    I'll start with a brute-force solution in hopes of inspiring others to beat it. Note that set membership can be done in constant time, after one pass through the set. See this question. Also note that this assumes all the elements of S are in fact in L, otherwise it will just return the sublist from 1 to n.

    best = (1,n)
    For i from 1 to n-k:  
      Create/reset a hash found[] mapping each element of S to False.
      For j from i to n or until counter == k:  
        If found[L[j]] then counter++ and let found[L[j]] = True;
      If j-i < best[2]-best[1] then let best = (i,j).
    

    Time complexity is O((n+k)(n-k)). Ie, n^2-ish.

提交回复
热议问题