Hi, here’s your problem today. This problem was recently asked by AirBNB:
Implement auto-completion. Given a large set of words (for instance 1,000,000 words) and then a single word prefix, find all words that it can complete to.
class Solution: def build(self): # Fill this in. def autocomplete(self, word): # Fill this in. s = Solution() s.build(['dog', 'dark', 'cat', 'door', 'dodge']) print(s.autocomplete('do')) # ['dog', 'door', 'dodge']
Can you solve this optimally (in linear time with regards to the result set)?