Backtracking
Medium

Word Search

Find if a word exists in a 2D grid of characters.
Problem 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).

  1. Iterate: Start DFS from every cell (r, c) in the grid.
  2. DFS Function: Check if board[r][c] matches the current character of word.
  3. Mark Visited: Temporarily mark the current cell (e.g., with # or a visited set) to prevent reusing it.
  4. Explore: Recursively search in all 4 directions (Top, Bottom, Left, Right).
  5. 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.
Unlock VisualizerPREMIUM FEATURE

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.

Start Your Premium Prep