79. Word Search¶
Edited by Charles
Medium · Python · Backtrack
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Use DFS + Backtracking.
Each cell in the board can be the starting point of the word.
Original study notes follow, including intermediate snippets and earlier attempts.
Idea¶
Use DFS + Backtracking.
Each cell in the board can be the starting point of the word.
If board[i][j] == word[0], we:
- Mark the starting cell as visited.
- Start DFS from
(i, j). - Search its four neighboring cells for the next character.
- Mark a selected cell as visited before recursion.
- Restore the cell after recursion (backtracking).
A cell cannot be used more than once in the same path.
Backtracking State¶
Define:
where:
(row, col)is the current position.word[0:startIndex]has already been matched.- We now search the neighbors of
(row, col)forword[startIndex].
For example, if:
and we have already found A:
then we call:
because word[0] = 'A' has already been matched, and we are now looking for:
Starting Point¶
Since the word can start from any cell, iterate through the entire board:
Once word[0] is found, mark that cell as visited and start searching for word[1]:
Search Four Directions¶
From the current position, search:
For each neighbor, check:
- It is inside the board.
- It has not been visited.
- Its character equals
word[startIndex].
if (
0 <= nextRow < m
and 0 <= nextCol < n
and board[nextRow][nextCol] != '.'
and board[nextRow][nextCol] == word[startIndex]
):
If all conditions are satisfied, continue searching from that cell.
Backtracking¶
Before entering a cell, temporarily mark it as visited:
Then recursively search for the next character:
After the recursive search finishes, restore the cell:
This is the key backtracking step:
Restoring the cell allows it to be used again in a different search path.
Base Case¶
When:
all characters have already been matched.
Therefore:
Example¶
For:
The search path is:
The recursive states are approximately:
Find A outside DFS
↓
backtrack(A, 1) # search for B
↓
backtrack(B, 2) # search for C
↓
backtrack(C, 3) # search for C
↓
backtrack(C, 4) # search for E
↓
backtrack(E, 5) # search for D
↓
backtrack(D, 6)
↓
startIndex == len(word)
↓
Found
Complexity¶
Let:
m × nbe the size of the board.Lbe the length ofword.
Each cell can be used as a starting point, and from each position we can explore up to four directions.
Time Complexity:
A tighter bound can be written as O(m × n × 3^L) because after the first move, we cannot immediately return to the previous cell.
Space Complexity:
The maximum recursion depth is the length of the word. The board itself is used to record visited cells, so no additional visited matrix is needed.
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | September 7, 2026 |
Python solution¶
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
self.directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
self.m, self.n = len(board), len(board[0])
self.res = False
for i in range(self.m):
for j in range(self.n):
if board[i][j] == word[0]:
temp, board[i][j] = board[i][j], '.'
self.backtrack(i, j, 1, board, word)
board[i][j] = temp
if self.res:
return True
return False
def backtrack(self, row, col, startIndex, board, word):
if startIndex == len(word):
self.res = True
return
for dir in self.directions:
nextRow = row + dir[0]
nextCol = col + dir[1]
if (
0 <= nextRow < self.m
and 0 <= nextCol < self.n
and board[nextRow][nextCol] != '.'
and board[nextRow][nextCol] == word[startIndex]
):
temp, board[nextRow][nextCol] = board[nextRow][nextCol], '.'
self.backtrack(
nextRow,
nextCol,
startIndex + 1,
board,
word
)
board[nextRow][nextCol] = temp