"Word Search II" is an extension of the classic word search grid problem. Instead of looking for a single word, you are given a 2D board of characters and a list of words. Your task is to find all the words from the list that can be constructed by sequentially adjacent cells in the board. A cell cannot be reused within a single word.
This is a standard "Hard" problem because it tests your ability to combine two major data structures/algorithms: Tries and DFS with Backtracking. Companies like Uber and Amazon ask this to see if you can optimize a search. If you ran a standard DFS for every word in the list, it would be extremely slow. Using a Trie allows you to search for multiple words simultaneously as you traverse the grid.
The optimal approach uses a Trie (Prefix Tree).
Board:
o a n n
e t a e
i h k r
Words: ["oath", "pea", "eat"].
o: In Trie? Yes.a: In Trie? Yes.t: In Trie? Yes.h: In Trie? Yes. Word oath found!e: In Trie? Yes.a: In Trie? Yes.t: In Trie? Yes. Word eat found!Practice implementing a Trie from scratch. For "Word Search II," make sure you understand how to "prune" the Trie—deleting nodes that no longer lead to any words—to keep the search space as small as possible.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Word Squares | Hard | Solve | |
| Word Search | Medium | Solve | |
| Longest Common Suffix Queries | Hard | Solve | |
| Path with Maximum Gold | Medium | Solve | |
| Words Within Two Edits of Dictionary | Medium | Solve |