Imagine you're solving a word search puzzle from your favorite magazine! Given an m × n grid filled with letters and a target word, your mission is to determine if the word exists somewhere in the grid.

The rules are simple but important:
• You can only move to adjacent cells (horizontally or vertically neighboring)
• Each letter cell can only be used once per word
• You need to find the complete word following a valid path

Return true if the word exists in the grid, false otherwise. This classic puzzle combines backtracking and depth-first search - perfect for practicing recursive algorithms!

Input & Output

example_1.py — Basic word found
$ Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
💡 Note: The word "ABCCED" can be found by starting at (0,0) and following the path: A(0,0) → B(0,1) → C(0,2) → C(1,2) → E(2,2) → D(2,1). Each cell is used exactly once.
example_2.py — Word not found
$ Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false
💡 Note: The word "ABCB" cannot be found. While we can start A(0,0) → B(0,1) → C(0,2), there's no way to reach another 'B' without reusing the B at (0,1), which violates the rule.
example_3.py — Single character
$ Input: board = [["A"]], word = "A"
Output: true
💡 Note: Simple case: the single cell contains the target word 'A', so it's found immediately.

Visualization

Tap to expand
Word Search: Finding "ABCCED"ASTARTBCESFCSADEEAlgorithm Steps:1. Find 'A' cells: (0,0) and (2,0)2. Try path from (0,0): A→B→C→C(below)→E→D ✓3. Mark cells as visited (green)4. Backtrack if path fails5. Unmark cells when backtrackingSuccess! Word found in gridValid path foundCells not in path?Unexplored options
Understanding the Visualization
1
Find starting points
Look for cells containing the first letter of your word
2
Explore paths
From each starting point, use DFS to explore adjacent cells
3
Mark territory
Mark each cell as visited to avoid reusing it in the current path
4
Backtrack smartly
If a path fails, unmark cells and try a different direction
5
Success or retry
Return true when word is complete, or try next starting point
Key Takeaway
🎯 Key Insight: Use DFS with backtracking, but be smart about starting points - only begin from cells that match the first character of your target word!

Time & Space Complexity

Time Complexity
⏱️
O(m × n × 4^L)

For each of the m×n cells, we potentially explore 4^L paths where L is word length

n
2n
Linear Growth
Space Complexity
O(L)

Recursion stack depth equals the length of the word

n
2n
Linear Space

Constraints

  • m == board.length
  • n = board[i].length
  • 1 ≤ m, n ≤ 6
  • 1 ≤ word.length ≤ 15
  • board and word consist of only lowercase and uppercase English letters
Asked in
Meta 45 Microsoft 38 Amazon 32 Google 28
52.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen