Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Hard Trie Medium frequency Solution Available Practice Mode

Word Search II LeetCode Solution

Tags: #Array #String #Backtracking #Trie #Matrix

Approach Summary

Insert all words into a Trie. DFS from each cell, following Trie edges. Mark found words to avoid duplicates. Prune dead Trie branches.

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Trie Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Trie pattern.

Framework: Trie • Interview Prep: Tested at FAANG • Explore all Trie problems & invariants →

How to Recognize This Pattern

  • "Find all words from a list in a grid"
  • Trie prunes search: abort branch if no word shares the prefix

Complexity Analysis

Time Complexity

O(M × N × 4 × 3^(L-1))

Space Complexity

O(∑ word lengths)

Tags

Array String Backtracking Trie Matrix

Frequently Asked Questions

What is the optimal time and space complexity for Word Search II?
The optimal solution for Word Search II achieves O(M × N × 4 × 3^(L-1)) runtime complexity and requires O(∑ word lengths) auxiliary space. Time complexity is O(M * N * 4^(3^(L-1))) bounded by board dimensions and word length L <= 10. Traversal terminates early whenever a prefix does not exist in the Trie. Space complexity is O(sum of word lengths) to store all words in the Trie.
How do I identify that Word Search II requires the Trie pattern?
In technical interviews, look for key problem signals: "Find all words from a list in a grid"; Trie prunes search: abort branch if no word shares the prefix. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Trie strategy.
What critical edge cases must be handled in Word Search II?
Essential edge cases to verify include: Words sharing common prefixes (e.g. "oath" and "pea"); Board smaller than target word lengths; No words found on board (returns empty list []); Duplicate words in words input (handled by Trie or Set). Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Word Search II?
Word Search II is a Hard-difficulty question tagged with medium recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Microsoft, Google.
Buy me a coffee
Daily Practice
0/0