The problem
Write a compressed trie (lesson 4): each edge carries a string, the labels below a node start with different letters, and every node except the root is the end of a word or has at least two children. Store edges as the starter does: node.kids maps the first letter of a label to the pair (label, child). insert, search and starts_with work as in TRIE-P1.
The method labels() is given. It lists the edge labels depth first, in alphabetical order, so the tests can see the shape of your trie.
t = RadixTrie()
for w in ["bus", "tea", "team", "ten", "to", "toast"]:
t.insert(w)
t.labels()
# ['bus', 't', 'e', 'a', 'm', 'n', 'o', 'ast']
t.starts_with("toa") # True: ends inside the edge 'ast'
t.search("toa") # False
Words and prefixes are non-empty and use the letters a to z. The same word may be inserted twice.
Limits
- Up to 20,000 words of at most 8 letters. Each operation should take O(L) time, and the trie must have at most 2n nodes for n different words.
Builds on TRIE-4 Compressed tries.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | The lesson's words | Not run | |
| 2 | Split an edge | Not run | |
| 3 | Word ends inside an edge | Not run | |
| 4 | One word | Not run | |
| 5 | Order does not matter | Not run | |
| 6 | Search inside an edge | Not run | |
| 7 | Inserted twice | Not run | |
| 8 | 20,000 words | Not run |