Backtracking
Medium
Word Search
Find if a word exists in a 2D grid of characters.
10 min read
Solve on LeetCodeProblem Understanding
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells (horizontally or vertically neighboring). The same letter cell may not be used more than once.
Algorithm Strategy
This is 2D Grid DFS (Backtracking).
- Iterate: Start DFS from every cell
(r, c)in the grid. - DFS Function: Check if
board[r][c]matches the current character ofword. - Mark Visited: Temporarily mark the current cell (e.g., with
#or avisitedset) to prevent reusing it. - Explore: Recursively search in all 4 directions (Top, Bottom, Left, Right).
- Backtrack: Unmark the cell before returning so other paths can use it.
Interactive Visualization
Step 1 / 1
Visualization data missing
Initializing...
1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.ON THIS PAGE
- Problem Understanding
- Algorithm Strategy
- Interactive Visualization
- Dry Run
- Edge Cases & Common Mistakes
- Solution
- Complexity Analysis
Stop Guessing, Start Mastering.
Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.
