Word Search II - Problem

Imagine you're a treasure hunter with a grid of letters and a list of secret words to find! Your mission is to discover which words are hidden in the grid by connecting adjacent letters.

Given an m x n board of characters and a list of strings words, return all words that can be found on the board. Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring (no diagonal moves allowed). The same letter cell may not be used more than once in a single word.

Example: If your board contains letters arranged in a grid and you're searching for words like "OATH" and "PEA", you need to trace a path through adjacent cells to spell out each word completely.

Input & Output

example_1.py β€” Basic Grid Search
$ Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
β€Ί Output: ["eat","oath"]
πŸ’‘ Note: "eat" can be formed by following path (1,1)β†’(1,2)β†’(0,2), and "oath" can be formed by (0,0)β†’(0,1)β†’(1,1)β†’(1,2). "pea" and "rain" cannot be formed on this board.
example_2.py β€” Single Character Grid
$ Input: board = [["a","b"],["c","d"]], words = ["abcb"]
β€Ί Output: []
πŸ’‘ Note: The word "abcb" cannot be formed because we cannot reuse the same cell. After using 'a'β†’'b', we cannot go back to use 'b' again.
example_3.py β€” Multiple Valid Words
$ Input: board = [["o","a","b","n"],["o","t","a","e"],["a","h","k","r"],["a","f","l","v"]], words = ["oa","oaa"]
β€Ί Output: ["oa","oaa"]
πŸ’‘ Note: Both "oa" (from (0,0)β†’(0,1)) and "oaa" (from (0,0)β†’(0,1)β†’(0,2)) can be found in the grid following valid adjacent paths.

Visualization

Tap to expand
πŸ—ΊοΈ Word Search Treasure HuntTreasure Map (Board)OATETHOATH Path Found! πŸ†Smart Compass (Trie)RootOEPA🧭 Guides efficient search🎯 The Magic of Trie + DFS1. Build Compass: Create Trie with all treasure words2. Smart Search: DFS from each cell, following Trie guidance3. Early Exit: Stop exploring if no words start with current pathResult: Find multiple treasures in a single efficient expedition! ⚑
Understanding the Visualization
1
Build the Compass
Create a Trie (prefix tree) containing all treasure words to guide our search
2
Start Exploration
Begin DFS from each cell on the board, following the Trie's guidance
3
Smart Pruning
Stop exploring paths early if the Trie indicates no words can be formed
4
Collect Treasures
When we reach a complete word in the Trie, add it to our treasure collection
Key Takeaway
🎯 Key Insight: The Trie data structure transforms this from multiple separate searches into one intelligent exploration, dramatically reducing the search space through smart pruning!

Time & Space Complexity

Time Complexity
⏱️
O(M Γ— N Γ— 4^L)

MΓ—N cells Γ— 4^L paths, where L is max word length. Trie allows early pruning.

n
2n
βœ“ Linear Growth
Space Complexity
O(K Γ— L)

Trie storage for K words with average length L, plus recursion stack

n
2n
βœ“ Linear Space

Constraints

  • m == board.length
  • n == board[i].length
  • 1 ≀ m, n ≀ 12
  • board[i][j] is a lowercase English letter
  • 1 ≀ words.length ≀ 3 Γ— 104
  • 1 ≀ words[i].length ≀ 10
  • words[i] consists of lowercase English letters
  • All the strings of words are unique
Asked in
Google 85 Amazon 72 Microsoft 58 Meta 45 Apple 38
89.4K Views
High Frequency
~25 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen