Data Structures19 sections · 729 units
Open in Course

Word Search II Solution

Implement your solution

Implement the complete solution with trie-guided backtracking. Implementation steps:

1.1. Build trie from word list

2.2. For each cell, start DFS with trie root

3.3. Track visited cells (modify board or use set)

4.4. When reaching a word-end node, add to results

5.5. Backtrack by restoring cell state Common bugs:

  • Forgetting to unmark visited cells when backtracking
  • Finding the same word multiple times (from different starting positions)
  • Not handling the case where one word is a prefix of another

Time: O(MN4L)O(M \cdot N \cdot 4^L) where M×NM \times N is board size and LL is max word length. Trie pruning makes this much faster in practice.