@Sravanreddy: Filling in a few details:
Find the first occurrence of each given word in the document. While
doing this, insert the location of each word into a min-heap, and keep
track of the maximum of all locations. Record the difference between
the top of the heap and the maximum as the smalles
parse the document for the words and maintain a position table
position table should be like this:
w1-> p1 p2 p3pn
w2->x1 x2 x3 xn
k lists
O(n) if hashing is used
now go to begining of list and findout the maximum position say
'start' O(k)
similarly from end of all list , check for th