PythiloLesson 19 of 32 · progress kept in this browser

Store words letter by letter in a trie

A trie is a tree of nested dictionaries, one letter per level. Words that share a prefix share the path for it, so finding every word that starts with "ca" means walking two steps and collecting what lies below.

root = {}
node = root
for letter in "cat":
    node = node.setdefault(letter, {})
node["$"] = True   # a word ends here
Your task

Write a Trie class with add(word), contains(word) and complete(prefix). complete returns every stored word starting with prefix, sorted.