This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 2: Structures
Share the prefixes so every word that starts the same way walks the same path.
Builds on Tree DFS.
The question to ask
Do I keep asking whether some string is a prefix of words I've stored?
A trie stores strings character by character down a tree, so every word sharing a prefix shares a path. Lookup and prefix checks cost O(length of the word), not O(number of words). Reach for one when the question cares about prefixes: autocomplete, "starts with", replacing words by their roots, or searching a board for many words at once, where one trie lets a single DFS check every word together.
Time O(L) per insert or lookup, for word length L · Space O(total characters)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Add and Search Words: an ordinary character follows one child, a '.' tries every child. A recursive helper with an index into the word keeps it short.
class DictNode {
children = new Map<string, DictNode>();
isWord = false;
}
class WordDictionary {
private root = new DictNode();
addWord(word: string): void {
let node = this.root;
for (const ch of word) {
let next = node.children.get(ch);
if (!next) {
next = new DictNode();
node.children.set(ch, next);
}
node = next;
}
node.isWord = true;
}
search(word: string): boolean {
const match = (node: DictNode, i: number): boolean => {
if (i === word.length) return node.isWord;
if (word[i] === ".") {
return [...node.children.values()].some((child) => match(child, i + 1));
}
const next = node.children.get(word[i]);
return next !== undefined && match(next, i + 1);
};
return match(this.root, 0);
}
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Find the longest common prefix string among an array of strings, or an empty string.
"Prefix" shared by every word. Comparing column by column is simplest; a trie walked while there's exactly one child is the generalisation.
Not attempted yet
Implement a trie with insert, search, and startsWith, which reports whether any stored word starts with a prefix.
"Trie" and "starts with" are in the title. Write the template until you can do it in five minutes.
Not attempted yet
Design a dictionary that adds words and searches for them, where a '.' in the search can match any letter.
A '.' that matches "any letter" branches over every child, which a trie makes cheap.
Not attempted yet
Given a dictionary of roots and a sentence, replace each word with the shortest root that is a prefix of it.
"Root" that is a "prefix": insert roots, walk each word until you hit an end marker.
Not attempted yet
As each character of a search word is typed, return up to three product suggestions that start with the typed prefix, in lexicographic order.
"Suggestions" for a growing "prefix": a trie with sorted words stored per node, or sort the products and binary search each prefix.
Not attempted yet
Implement a map that inserts key-value pairs and returns the sum of values of all keys starting with a given prefix.
Sum by "prefix": store a running sum on each trie node, adjusted by the difference when a key is updated.
Not attempted yet
Given a board of letters and a list of words, return every word that can be traced through adjacent cells without reusing a cell.
A "list of words" on one board: put them all in a trie so one DFS from each cell follows every word at once, pruning dead prefixes.
Not attempted yet
Return every word in the list that is a concatenation of at least two shorter words from the same list.
Built by "concatenation" of other words: walk a trie and restart from the root at each word end. (Word Break DP per word is the other route.)
Not attempted yet