Trie applications: autocomplete, word search, XOR maximum
Tries power autocomplete, word search on a grid, word replacement and the maximum XOR of two numbers.
Before you start: Tries: insert, search, prefix.
The idea
A trie answers "what starts with this?" fast. Three uses follow: autocomplete, word search on a grid and maximum XOR.
Intuition
Second example, word search: the trie tells the walk when a board path cannot grow into a word. Here c, a, r follows the trie to the pink end of car.
Without the trie, the walk would build every path on the board and check each against the word list.
How it works
For maximum XOR, write each number in binary and store its bits as a path. The best partner has opposite bits, high bits first.
A 1 in a high bit is worth more than every lower bit together, so the walk settles the high bits first.
Complexity
Checking every pair costs about n² ÷ 2. The trie walks one path of b bits per number, so the cost is about n × b.
Autocomplete costs the prefix length plus the words below it. Word search costs at most the longest word at each start cell.
Pitfalls
Word oata: the path o, a, t would need the a cell again, but a cell is used once per word. Free it when the search steps back.
Variations
Replace words walks each word of the sentence down a trie of roots and stops at the first end mark. cattle becomes cat, its shortest root.
Code
Pseudocode
best(x)
node ← root; result ← 0
for each bit from the top down
b ← this bit of x
if node.child[1 - b] exists
set this bit of result; node ← node.child[1 - b]
else node ← node.child[b]
return result
Python
def best_xor(root, x, bits):
node, result = root, 0
for i in range(bits - 1, -1, -1):
b = (x >> i) & 1
if (1 - b) in node:
result |= 1 << i
node = node[1 - b]
else:
node = node[b]
return result
JavaScript
function bestXor(root, x, bits) {
let node = root;
let result = 0;
for (let i = bits - 1; i >= 0; i--) {
const b = (x >> i) & 1;
if (node[1 - b]) {
result |= 1 << i;
node = node[1 - b];
} else {
node = node[b];
}
}
return result;
}
Practice problems (5)
Replace words Warm-up
A dictionary holds root words. In sentence, replace every word that starts with a root by the shortest root it starts with. A word with no root stays as it is.
Return the new sentence. Words are separated by single spaces.
Autocomplete top-3 Standard
Each word in words has a usage count in counts at the same index. Given a prefix, return the three most used words that start with it, most used first. Break ties alphabetically. Return fewer than three words when fewer match. An empty prefix matches every word.
The words are all different.
Maximum XOR pair Standard
Given nums, return the largest value of nums[i] XOR nums[j] over all pairs with i ≤ j. A number paired with itself gives 0. For an empty array, return 0.
Store the numbers as 30-bit paths in a binary trie. For each number, walk the trie and prefer the opposite bit at every level.
Trace the XOR walk Standard
The numbers 3, 10, 5, 25, 2 and 8 are stored as 5-bit paths in a binary trie. Follow the walk that finds the best XOR partner for 5 and answer before each step plays.
Word search on a board Challenge
A board is a list of equal-length strings, one per row. Return every word of words that can be spelled by a path of adjacent cells. Adjacent means up, down, left or right. A cell may be used only once per word. The result may be in any order.
Put the words in a trie and walk the board with DFS. Stop a path as soon as the trie has no edge for the next letter.
Related topics
Next on the path: Disjoint set union: find, union, rank, path compression