Ten problems that drill the trie pattern from Lesson 20. Build the dict-of-dicts trie once and most of these become small variations — a sentinel end-marker here, a DFS branch there. Each solution here was actually run and tested.
How to practise. For each problem, first decide what the nodes store (just children? a word-end flag? a stored value? a whole word?) — that decision is the problem. Then write the walk. If you stall for more than ~20 minutes, read the approach, understand it, and re-solve from memory the next day.
The 10 problems
- 1Longest Common Prefix (LeetCode 14)EasyFind the longest prefix shared by every string: build a trie and walk down while there is only one branch.arrow_forward
- 2Implement Trie (Prefix Tree) (LeetCode 208)MediumBuild insert, search and startsWith for a trie: one character walk over a dict of dicts with an end marker.arrow_forward
- 3Design Add and Search Words Data Structure (LeetCode 211)MediumStore words and search with '.' wildcards: a trie where a dot branches the DFS into every child.arrow_forward
- 4Replace Words (LeetCode 648)MediumReplace each word by its shortest dictionary root: put the roots in a trie and stop at the first end marker.arrow_forward
- 5Map Sum Pairs (LeetCode 677)MediumInsert key-value pairs and sum the values of all keys with a given prefix, using a trie plus a value map.arrow_forward
- 6Longest Word in Dictionary (LeetCode 720)MediumFind the longest word built one letter at a time from other words: sort, then grow a set of buildable words.arrow_forward
- 7Search Suggestions System (LeetCode 1268)MediumAfter each typed letter, suggest up to three products with that prefix: sort, then binary-search the prefix.arrow_forward
- 8Maximum XOR of Two Numbers in an Array (LeetCode 421)MediumFind the largest XOR of any pair: store numbers as bit paths in a trie and greedily take the opposite bit.arrow_forward
- 9Short Encoding of Words (LeetCode 820)MediumShortest reference string for a word list: insert words reversed into a trie and count only the leaves.arrow_forward
- 10Word Search II (LeetCode 212)HardFind every dictionary word on a letter grid: put words in a trie and let it prune one shared grid DFS.arrow_forward