Skip to content

Instantly share code, notes, and snippets.

@vivekn
Last active October 1, 2015 02:58
Show Gist options
  • Save vivekn/1906922 to your computer and use it in GitHub Desktop.
Save vivekn/1906922 to your computer and use it in GitHub Desktop.
Tries 2
class Trie(object):
...
def all_suffixes(self, prefix):
results = set()
if self.flag:
results.add(prefix)
if not self.children: return results
return reduce(lambda a, b: a | b, [node.all_suffixes(prefix + char) for (char, node) in self.children.items()]) | results
def autocomplete(self, prefix):
node = self
for char in prefix:
if char not in node.children:
return set()
node = node.children[char]
return list(node.all_suffixes(prefix))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment