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.
Example 1:
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"]
Example 2:
Input: board = [["a","b"],["c","d"]], words = ["abcb"] Output: []
Constraints:
m == board.lengthn == board[i].length1 <= m, n <= 12board[i][j] is a lowercase English letter.1 <= words.length <= 3 * 1041 <= words[i].length <= 10words[i] consists of lowercase English letters.words are unique.Problem Overview: You get an m x n board of characters and a list of words. A word can be formed by moving horizontally or vertically across adjacent cells without reusing the same cell in one path. The task is to return every word from the list that exists in the grid.
Approach 1: Backtracking and HashSet (O(W * m * n * 4^L) time, O(L) space)
This approach treats each word independently. For every word in the dictionary, start a DFS from every board cell that matches the first character. Use backtracking to explore four directions (up, down, left, right) while marking cells as visited so they are not reused in the same path. If the DFS successfully matches all characters of the word, add it to a result HashSet to avoid duplicates. The main drawback is repeated work: many prefixes overlap between words, yet the search restarts from scratch for each one. The worst‑case time complexity becomes O(W * m * n * 4^L), where W is the number of words and L is the maximum word length. Space complexity is O(L) for the recursion stack.
This method is straightforward and easy to implement, but it becomes slow when the word list is large because the board is scanned repeatedly for similar prefixes.
Approach 2: Backtracking with Trie (O(m * n * 4^L) time, O(N) space)
The optimized approach builds a Trie from the list of words before exploring the grid. A Trie stores shared prefixes, so multiple words that begin with the same characters reuse the same search path. Start DFS from each cell in the board, but only continue exploring if the current prefix exists in the Trie. If a Trie node marks the end of a word, add that word to the result immediately.
This pruning drastically reduces unnecessary exploration. If the board path forms a prefix that doesn't exist in the Trie, the search stops early. Each DFS step checks a child node in the Trie, which is an O(1) lookup. The search still explores up to four directions per step, leading to worst‑case complexity O(m * n * 4^L), but in practice it is much faster due to prefix pruning. Space complexity is O(N) for the Trie, where N is the total number of characters across all words.
This technique combines backtracking with a Trie to efficiently share prefix searches. The board traversal itself is a typical matrix DFS problem.
Recommended for interviews: Interviewers expect the Trie + backtracking solution. The HashSet approach demonstrates understanding of DFS search on a grid, but it repeats work for overlapping prefixes. Using a Trie shows that you can optimize prefix searches and reduce redundant exploration, which is the key insight for this problem.
This approach uses a Trie to represent the list of words for efficient prefix matching. The algorithm performs depth-first search (DFS) on the board starting from each cell. Each path of exploration is checked against the Trie to determine if it forms any of the words or prefixes thereof. The benefit of using a Trie is that we can stop the search early if the current path does not match any prefix in the Trie, thereby improving the efficiency of the search process.
This C solution constructs a Trie for the list of words and uses a recursive DFS to explore each board cell. During the exploration, it checks for word matches and adds them to the result if found. The Trie structure is dynamically built using a set of Trie nodes. The DFS ensures not to visit the same cell multiple times within the same exploration path.
Time Complexity: O(M*(4*3^(L-1))), where M is the number of cells on the board, and L is the maximum length of a word.
Space Complexity: O(N*L), where N is the number of words, and L is the maximum length of a word, used by the Trie structure.
In this approach, we perform backtracking for each word individually. This avoids the overhead of constructing a Trie but involves repeated board scanning. A Recursive Depth First Search (DFS) is used to explore possibilities starting from each cell on the board. We maintain a visited matrix to ensure a letter is not reused in forming a particular word.
This C solution attempts to find each word on the board individually. We use a helper function dfs that tries to construct the word by moving to adjacent cells recursively. The solution checks for existence of a path for each word, returning as soon as one is found.
Time Complexity: O(N * M * 3^L), where N is the number of words, M is the number of cells in the board, and L is the length of the word being searched in the worst case.
Space Complexity: O(L), for the recursive call stack.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Using Backtracking with Trie | Time Complexity: O(M*(4*3^(L-1))), where M is the number of cells on the board, and L is the maximum length of a word. |
| Using Backtracking and HashSet | Time Complexity: O(N * M * 3^L), where N is the number of words, M is the number of cells in the board, and L is the length of the word being searched in the worst case. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking and HashSet | O(W * m * n * 4^L) | O(L) | Small word lists or when implementing a simple DFS per word |
| Backtracking with Trie | O(m * n * 4^L) | O(N) | Best general solution when many words share prefixes |
Word Search II - Backtracking Trie - Leetcode 212 - Python • NeetCode • 209,690 views views
Watch 9 more video solutions →Practice Word Search II with our built-in code editor and test cases.
Practice on FleetCode