Tuesday, May 8, 2012

Length of the shortest phrase with given keywords in a large text

This question was asked to one of my friends in an interview.



Given two keywords, we have to find the length of the shortest phrase with the given keywords in a large text.
The keywords may appear in any order in that text.
Constraint : Keep an efficient data structure so that each time the text need not be parsed for queries with different keywords




eg. keywords: "one", "four" text: "one two three four five four six
one"



here the shortest such phrase is "four six one" rather than "one two
three four"




The solution we have in mind is:
Built a BST with all the words of the text. Each node maintains the locations of the words.
(This will be a sorted list) When a query comes search [O(logn)] both words, Find the minimum difference between their locations in [O(n)] Thus making it effectively [O (nlogn)].



Can we do better ?





No comments:

Post a Comment