Try it first — give it 40 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get an m × n grid board of lowercase letters (at most 12 × 12) and a list of distinct words (up to 3 × 10⁴ of them, each at most 10 letters long). Return every word that appears in the grid.
A word "appears" if you can spell it by starting at some cell and stepping to a horizontally or vertically adjacent cell for each next letter, never using the same cell twice in one word. Return the found words in any order.
Example:
o a a n
e t a e
i h k r
i f l vwords = ["oath", "pea", "eat", "rain"] → ["oath", "eat"]. "oath" goes o(0,0) → a(0,1) → t(1,1) → h(2,1). "eat" goes e(1,3) → a(1,2) → t(1,1). There's no p, so no "pea", and "rain" can't be traced.
Start with brute force
You already know Word Search, from the Backtracking & Graphs stage: given one word, DFS from every cell, matching letter by letter, and backtrack.
So run it once per word. Each run starts from up to 144 cells and can branch three ways at each of up to 9 further steps (four directions, minus the one you came from). That's up to 144 × 4 × 3⁹ ≈ 11 million steps per word, and there can be 3 × 10⁴ words. Even with typical early exits, words that share prefixes like "oat", "oath" and "oaths" redo the same grid exploration again and again.
Building the intuition
The waste is that every word walks the grid separately. Two words starting with "oa" both explore every "o → a" path in the grid.
Turn it around. Do one backtracking search over the grid, and at every step ask: "Is the path I've spelled so far the beginning of any word?" If not, stop. If the path spells a whole word, record it.
"Is this the beginning of any word?" is startsWith, and a trie answers it. Even better, you don't have to restart the lookup from the root each step. As the DFS adds one letter to its path, the trie pointer moves one child down. So the DFS carries two positions at once: a cell in the grid and a node in the trie.
- Step to a neighbour only if its letter is a child of the current trie node. That one check is the whole pruning.
- If the trie node marks the end of a word, you've found that word.
The trie also makes shared prefixes free. "oat", "oath" and "oaths" share one path, explored once.
The optimal approach
- Build a trie of all the words. At each word's last node, store the word itself (
node.word = "oath") instead of a plain true flag. Then you never have to rebuild the string from the path. - Start a DFS from every cell whose letter is a child of the root.
dfs(r, c, parent):- Let
ch = board[r][c]andnode = parent.children[ch]. - If
node.wordis set, add it to the answer and clear it, so it can't be added twice. - Mark the cell visited by setting it to
#, which is never a trie key, so it can't be stepped on again. - For each of the 4 neighbours inside the grid whose letter is in
node.children, recurse withnodeas the new parent. - Restore
board[r][c] = ch. - Prune: if
nodenow has no children and no word, delete it fromparent.children.
- Let
- Return the collected words.
Three details make this both correct and fast:
- Clearing
node.word. A word can be traced along several paths, or from several starts. Without clearing, it would show up more than once. - Marking by overwriting the cell. There's no separate visited array, and because
#is never a child, the "is this letter a child?" check skips visited cells for free. - Pruning finished branches. Once every word under a node has been found, that node is useless. Deleting it means later DFS calls don't even step in. On grids full of one repeated letter, this is the difference between passing and timing out.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class TrieNode:
def __init__(self):
self.children = {} # letter -> TrieNode
self.word = None # the whole word that ends at this node, if any
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
root = TrieNode()
for w in words:
node = root
for ch in w:
if ch not in node.children:
node.children[ch] = TrieNode()
node = node.children[ch]
node.word = w # store the word itself, so we never rebuild it from the path
rows, cols = len(board), len(board[0])
found = []
def dfs(r: int, c: int, parent: TrieNode) -> None:
ch = board[r][c]
node = parent.children[ch]
if node.word is not None:
found.append(node.word)
node.word = None # report each word once, even if it appears twice
board[r][c] = "#" # mark visited; "#" is never a trie key
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nr, nc = r + dr, c + dc
# Only step where the trie says some word can continue
if 0 <= nr < rows and 0 <= nc < cols and board[nr][nc] in node.children:
dfs(nr, nc, node)
board[r][c] = ch # un-mark for other paths
# Prune: a node with no words left below it can never help again
if not node.children and node.word is None:
del parent.children[ch]
for r in range(rows):
for c in range(cols):
if board[r][c] in root.children:
dfs(r, c, root)
return foundstruct TrieNode {
unordered_map<char, TrieNode*> children; // letter -> child
string word; // the whole word that ends here ("" if none)
};
class Solution {
int rows = 0, cols = 0;
vector<string> found;
void dfs(vector<vector<char>>& board, int r, int c, TrieNode* parent) {
char ch = board[r][c];
TrieNode* node = parent->children[ch];
if (!node->word.empty()) {
found.push_back(node->word);
node->word.clear(); // report each word once, even if it appears twice
}
board[r][c] = '#'; // mark visited; '#' is never a trie key
int dr[4] = {1, -1, 0, 0}, dc[4] = {0, 0, 1, -1};
for (int d = 0; d < 4; d++) {
int nr = r + dr[d], nc = c + dc[d];
// Only step where the trie says some word can continue
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && node->children.count(board[nr][nc])) {
dfs(board, nr, nc, node);
}
}
board[r][c] = ch; // un-mark for other paths
// Prune: a node with no words left below it can never help again
if (node->children.empty() && node->word.empty()) {
parent->children.erase(ch);
delete node;
}
}
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
TrieNode* root = new TrieNode();
for (const string& w : words) {
TrieNode* node = root;
for (char ch : w) {
if (!node->children.count(ch)) {
node->children[ch] = new TrieNode();
}
node = node->children[ch];
}
node->word = w; // store the word itself, so we never rebuild it from the path
}
rows = board.size();
cols = board[0].size();
found.clear();
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (root->children.count(board[r][c])) {
dfs(board, r, c, root);
}
}
}
return found;
}
};class Solution {
private static class TrieNode {
Map<Character, TrieNode> children = new HashMap<>(); // letter -> child
String word = null; // the whole word that ends here, if any
}
private static final int[][] DIRS = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
private int rows, cols;
private List<String> found;
public List<String> findWords(char[][] board, String[] words) {
TrieNode root = new TrieNode();
for (String w : words) {
TrieNode node = root;
for (char ch : w.toCharArray()) {
node = node.children.computeIfAbsent(ch, k -> new TrieNode());
}
node.word = w; // store the word itself, so we never rebuild it from the path
}
rows = board.length;
cols = board[0].length;
found = new ArrayList<>();
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (root.children.containsKey(board[r][c])) {
dfs(board, r, c, root);
}
}
}
return found;
}
private void dfs(char[][] board, int r, int c, TrieNode parent) {
char ch = board[r][c];
TrieNode node = parent.children.get(ch);
if (node.word != null) {
found.add(node.word);
node.word = null; // report each word once, even if it appears twice
}
board[r][c] = '#'; // mark visited; '#' is never a trie key
for (int[] d : DIRS) {
int nr = r + d[0], nc = c + d[1];
// Only step where the trie says some word can continue
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && node.children.containsKey(board[nr][nc])) {
dfs(board, nr, nc, node);
}
}
board[r][c] = ch; // un-mark for other paths
// Prune: a node with no words left below it can never help again
if (node.children.isEmpty() && node.word == null) {
parent.children.remove(ch);
}
}
}class TrieNode {
constructor() {
this.children = new Map(); // letter -> TrieNode
this.word = null; // the whole word that ends at this node, if any
}
}
/**
* @param {character[][]} board
* @param {string[]} words
* @return {string[]}
*/
var findWords = function (board, words) {
const root = new TrieNode();
for (const w of words) {
let node = root;
for (const ch of w) {
if (!node.children.has(ch)) {
node.children.set(ch, new TrieNode());
}
node = node.children.get(ch);
}
node.word = w; // store the word itself, so we never rebuild it from the path
}
const rows = board.length, cols = board[0].length;
const DIRS = [[1, 0], [-1, 0], [0, 1], [0, -1]];
const found = [];
const dfs = (r, c, parent) => {
const ch = board[r][c];
const node = parent.children.get(ch);
if (node.word !== null) {
found.push(node.word);
node.word = null; // report each word once, even if it appears twice
}
board[r][c] = "#"; // mark visited; "#" is never a trie key
for (const [dr, dc] of DIRS) {
const nr = r + dr, nc = c + dc;
// Only step where the trie says some word can continue
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && node.children.has(board[nr][nc])) {
dfs(nr, nc, node);
}
}
board[r][c] = ch; // un-mark for other paths
// Prune: a node with no words left below it can never help again
if (node.children.size === 0 && node.word === null) {
parent.children.delete(ch);
}
};
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (root.children.has(board[r][c])) {
dfs(r, c, root);
}
}
}
return found;
};Dry run
The example grid, with words = ["oath", "pea", "eat", "rain"]. The trie's root has children o, p, e and r.
| Step | Cell | Letter | Trie node | What happens |
|---|---|---|---|---|
| 1 | (0,0) | o | o | root has o → DFS starts here; mark (0,0) |
| 2 | (0,1) | a | oa | neighbour a is a child of o → go |
| 3 | (1,1) | t | oat | t is a child of oa → go. (0,2)'s a isn't a child of oa, so it's skipped |
| 4 | (2,1) | h | oath | node.word = "oath" → record it, clear it |
| 5 | — | — | — | oath has no children and no word left → prune it. As the calls return, oat, oa and o are now empty too, so they're pruned |
| 6 | (0,1), (0,2), … | a, n | — | root has no a or n → never start there |
| 7 | (1,0) | e | e | neighbours o, i and t: none is a, the only child of e → dead end |
| 8 | (1,3) | e | e | neighbour (1,2) a → ea, then (1,1) t → eat: record it |
| 9 | (2,3) | r | r | neighbours e, k, v: none is a → dead end |
Answer: ["oath", "eat"]. Notice step 3: the search never even looked down paths like "oaa", because no word starts that way.
Complexity
- Time: O(m · n · 4 · 3^(L−1)) in the worst case, where L is the longest word (10 here). Each start cell can go 4 ways, then at most 3 ways (you can't go back) at each later step, and the trie stops a path once it leaves every word. Building the trie adds O(total letters in words). In practice, pruning and shared prefixes keep it far below the bound.
- Space: O(total letters in words) for the trie, plus O(L) recursion depth. The board is reused for visited marks.
Edge cases and traps
- Duplicates in the output. The same word found twice, along two paths or from two starts. Clear
node.wordwhen you record it. - Stopping at the first word on a path. Finding "oat" must not end the DFS, because "oath" may continue from there. Record it and keep going.
- Forgetting to restore the cell. Then later paths think it's visited, and you miss words.
- Reusing a cell within one word. "abcb" on
[[a, b], [c, d]]would need thebcell twice. The#mark prevents that. - One-cell board.
[["a"]]with["a"]finds "a" right at the start cell, before any neighbour step. - Pruning while iterating the children. We only delete a child after its own DFS returns, and the loop runs over directions, not over the children map, so nothing is modified mid-iteration.
Say it in the interview
"Running Word Search for each word repeats the same grid exploration, so I'll put all the words in a trie and do one backtracking pass. The DFS carries a grid cell and a trie node together. I only step to a neighbour if its letter is a child of the current node, so any path that isn't a prefix of some word dies immediately. Each word is stored at its last trie node. When I reach it, I record it and clear it, so I don't add it twice. I mark cells visited by overwriting them and restore them on the way back, and I prune trie nodes once nothing is left below them. The worst case is m times n times 4 times 3 to the L minus 1, but the trie keeps it far lower in practice."
Follow-ups interviewers ask
- "Why is the trie better than Word Search per word?" Words sharing a prefix share one exploration, and a path stops as soon as it isn't a prefix of any word, not just of the current one.
- "How do you make it faster still?" Prune found branches (done above). You can also skip words containing a letter the board doesn't have, or that use a letter more often than the board does.
- "What if diagonal moves are allowed?" Use 8 directions. The algorithm is unchanged; the branching factor goes up.
Practise the same idea
This is a bonus day of the free DSA plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free