Improving the algorithm for looking up search terms

For Quora's problem "TypeAhead".
Still getting a timeout for some test cases.