Try it first — give it 25 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 letters and a string word. Can you spell word by starting at some cell and repeatedly stepping to a cell directly up, down, left or right (not diagonal)? Each cell can be used at most once in the path. Return true or false.
Limits: the grid is at most 6 × 6, and the word is at most 15 letters. Letters are upper and lower case English.
Example, with this board:
A B C E
S F C S
A D E E"ABCCED"→true: A(0,0) → B(0,1) → C(0,2) → C(1,2) → E(2,2) → D(2,1)."SEE"→true: S(1,3) → E(2,3) → E(2,2)."ABCB"→false: after A B C, the only B next to that C is the one already used.
Start with brute force
List every simple path in the grid (every path that doesn't repeat a cell), read off its letters, and compare with word. On a 6 × 6 grid the number of such paths is astronomically large. Most of them are useless: if the first letter is wrong, nothing that extends the path can help.
The improvement is to build paths one letter at a time and abandon a path the moment a letter doesn't match. That's backtracking, and it's what the optimal solution does.
Building the intuition
Say you're standing on a cell and you've matched the first k letters. What do you need now? A neighbouring cell holding word[k] that you haven't used yet. If there is one, step on it and ask the same question for word[k + 1]. That's the same problem again, one letter shorter, so it's recursion.
So the recursive function is dfs(r, c, k): "can I spell word[k:] starting at cell (r, c)?"
- If
board[r][c] != word[k]: no. - If
kis the last index: yes, the whole word is matched. - Otherwise: yes, if any of the 4 neighbours can spell
word[k + 1:].
Now the "no reuse" rule. You need to know which cells the current path has stepped on. The important word is current. Look at this board with the word "ABCESEEEFS":
A B C E
S F E S
A D E EThe search may first wander through some E cells on a path that fails. A different path, tried later, needs those same E cells. If you left them marked as used, you can wrongly return false. The answer here is true, but a version that never unmarks, trying directions in the order used below, returns false. So "visited" can't be a permanent set like in Number of Islands. It must be undone when you leave a cell.
That's the choose / explore / un-choose template from Subsets, laid on a grid:
- Choose: mark
(r, c)as visited. - Explore: try the 4 neighbours.
- Un-choose: unmark
(r, c).
The cheapest way to mark a cell is to overwrite it with a character that can never match, like '#', and write the real letter back afterwards. No extra set is needed.
The optimal approach
- For every cell
(r, c), ifdfs(r, c, 0)is true, returntrue. dfs(r, c, k):- If
board[r][c] != word[k], returnfalse. A'#'cell never matches, so this also blocks reuse. - If
k == len(word) − 1, returntrue. - Save the letter, set
board[r][c] = '#'. - For each of the 4 directions, if the neighbour is inside the grid and
dfs(neighbour, k + 1)is true, stop:found = true. - Put the saved letter back, and return
found.
- If
- If no start works, return
false.
The subtle detail: restore the cell on every exit, including success. If you return true straight from inside the loop, the board is left with '#' in it. The answer is still right, but you've silently damaged the caller's input. Setting found and restoring once at the end keeps the board unchanged.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
rows, cols = len(board), len(board[0])
directions = ((1, 0), (-1, 0), (0, 1), (0, -1)) # down, up, right, left
def dfs(r: int, c: int, k: int) -> bool:
# Can word[k:] be spelled starting at cell (r, c)?
if board[r][c] != word[k]:
return False
if k == len(word) - 1:
return True # matched the last letter
saved = board[r][c]
board[r][c] = "#" # choose: mark visited so this path can't reuse the cell
found = False
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols and dfs(nr, nc, k + 1):
found = True
break
board[r][c] = saved # un-choose: other paths may still use this cell
return found
for r in range(rows):
for c in range(cols):
if dfs(r, c, 0):
return True
return Falseclass Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
int rows = board.size(), cols = board[0].size();
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
if (dfs(board, word, r, c, 0)) return true;
return false;
}
private:
// Can word[k..] be spelled starting at cell (r, c)?
bool dfs(vector<vector<char>>& board, const string& word, int r, int c, int k) {
if (board[r][c] != word[k]) return false;
if (k == (int)word.size() - 1) return true; // matched the last letter
static const int dr[4] = {1, -1, 0, 0}; // down, up, right, left
static const int dc[4] = {0, 0, 1, -1};
int rows = board.size(), cols = board[0].size();
char saved = board[r][c];
board[r][c] = '#'; // choose: mark visited so this path can't reuse the cell
bool found = false;
for (int d = 0; d < 4 && !found; d++) {
int nr = r + dr[d], nc = c + dc[d];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && dfs(board, word, nr, nc, k + 1))
found = true;
}
board[r][c] = saved; // un-choose: other paths may still use this cell
return found;
}
};class Solution {
private static final int[][] DIRECTIONS = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; // down, up, right, left
public boolean exist(char[][] board, String word) {
for (int r = 0; r < board.length; r++)
for (int c = 0; c < board[0].length; c++)
if (dfs(board, word, r, c, 0)) return true;
return false;
}
// Can word[k..] be spelled starting at cell (r, c)?
private boolean dfs(char[][] board, String word, int r, int c, int k) {
if (board[r][c] != word.charAt(k)) return false;
if (k == word.length() - 1) return true; // matched the last letter
char saved = board[r][c];
board[r][c] = '#'; // choose: mark visited so this path can't reuse the cell
boolean found = false;
for (int[] d : DIRECTIONS) {
int nr = r + d[0], nc = c + d[1];
if (nr >= 0 && nr < board.length && nc >= 0 && nc < board[0].length
&& dfs(board, word, nr, nc, k + 1)) {
found = true;
break;
}
}
board[r][c] = saved; // un-choose: other paths may still use this cell
return found;
}
}/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
var exist = function (board, word) {
const rows = board.length, cols = board[0].length;
const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]; // down, up, right, left
// Can word[k..] be spelled starting at cell (r, c)?
const dfs = (r, c, k) => {
if (board[r][c] !== word[k]) return false;
if (k === word.length - 1) return true; // matched the last letter
const saved = board[r][c];
board[r][c] = "#"; // choose: mark visited so this path can't reuse the cell
let found = false;
for (const [dr, dc] of directions) {
const nr = r + dr, nc = c + dc;
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && dfs(nr, nc, k + 1)) {
found = true;
break;
}
}
board[r][c] = saved; // un-choose: other paths may still use this cell
return found;
};
for (let r = 0; r < rows; r++)
for (let c = 0; c < cols; c++)
if (dfs(r, c, 0)) return true;
return false;
};Dry run
The example board, word = "ABCCED". The directions are tried in the order down, up, right, left. Cells are written as (row, col).
| Call | Cell letter | Needs | Result |
|---|---|---|---|
dfs(0,0,0) | A | A | match, mark #; try down |
dfs(1,0,1) | S | B | no |
| (up is off the grid) | try right | ||
dfs(0,1,1) | B | B | match, mark; down is F |
dfs(1,1,2) | F | C | no; up is off the grid; try right |
dfs(0,2,2) | C | C | match, mark; try down |
dfs(1,2,3) | C | C | match, mark; try down |
dfs(2,2,4) | E | E | match, mark; down is off; up is #; right is E, needs D: no; try left |
dfs(2,1,5) | D | D | match, last letter → true |
Each call returns true back up the chain. On the way, each one writes its letter back, so the board ends exactly as it began.
Complexity
Let L be the length of the word.
- Time: O(m · n · 3^L). There are
m · nstarting cells. From each, the first step has 4 directions. Every later step has at most 3, because the cell you came from is marked. That's at most 4 · 3^(L−1) paths per start. - Space: O(L) for the recursion depth. Marking in place needs no visited set.
Edge cases and traps
- A visited set that's never cleared. It's correct on the easy examples and wrong on boards where two paths share cells. Always unmark on the way out.
- Checking bounds after indexing. Check
0 <= nr < rowsfirst, then readboard[nr][nc]. - Returning before restoring. It leaves
'#'in the board. Restore on every path out. - A word longer than the board. If
len(word) > m · n, the answer isfalseat once. That's worth checking up front. - A single cell.
[["a"]]with"a"must returntrue. The "last index" check runs before any neighbour is tried, so it does.
Say it in the interview
"I'll try every cell as a start and run a DFS that matches one letter per step. dfs(r, c, k) fails if the cell isn't word[k], and succeeds if k is the last index. Otherwise I mark the cell with a sentinel so this path can't reuse it, try the four neighbours with k + 1, and then restore the letter. Visited has to be per path, because a cell used by a failed path may be needed by another one. That's O(m·n·3^L) time, since each step after the first has at most three new directions, and O(L) space for the recursion."
Follow-ups interviewers ask
- "How would you prune further?" Count letters first. If the board has fewer of some letter than the word needs, return
falsestraight away. Also, if the word's last letter is rarer on the board than its first, search for the reversed word, so dead ends happen sooner. - "What if you have many words to find?" That's Word Search II in the Tries bonus stage. Put all the words in a trie and run a single DFS from each cell, walking down the trie as you go.
- "Can you avoid modifying the input?" Use a separate
visitedboolean grid with the same mark and unmark steps. It costs O(m · n) extra space.
Practise the same idea
This is Day 72 of a free 90-day 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