Word Search II - Problem
Given an m x n board of characters and a list of strings words, return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Input & Output
Example 1 — Basic Word 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: e(1,0)→a(1,2)→t(1,1). "oath" can be formed: o(0,0)→a(0,1)→t(1,1)→h(2,1). "pea" and "rain" cannot be formed on this board.
Example 2 — Single Character Board
$
Input:
board = [["a","b"],["c","d"]], words = ["abcb"]
›
Output:
[]
💡 Note:
"abcb" cannot be formed because we cannot reuse the same cell. After a→b→c, we cannot go back to use 'b' again.
Example 3 — Multiple Short Words
$
Input:
board = [["a","a"]], words = ["a","aa","aaa"]
›
Output:
["a","aa"]
💡 Note:
"a" can be formed using either cell. "aa" can be formed using both cells a(0,0)→a(0,1). "aaa" cannot be formed as we only have 2 'a' characters.
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
Visualization
Tap to expand
Understanding the Visualization
1
Input
2D board of characters and list of target words
2
Process
Search for each word using DFS with backtracking on adjacent cells
3
Output
List of all words found on the board
Key Takeaway
🎯 Key Insight: Use Trie data structure to eliminate redundant prefix searches and enable efficient multi-word detection in a single traversal
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code