Tries store strings by shared prefixes; great for autocomplete and prefix counting.
class Node: def __init__(self): self.next = {} self.end = False
class Trie: def __init__(self): self.root = Node() def insert(self, s): n = self.root for c in s: n = n.next.setdefault(c, Node()) n.end = True
# usage: insert many words, then traverse by prefix for suggestions