Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Backtracking High frequency Solution Available Practice Mode

Word Search LeetCode Solution

Tags: #Array #Matrix #Backtracking #DFS

Approach Summary

DFS from every cell. Mark cell as visited by temporarily changing its value. Restore on backtrack.

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Backtracking Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Backtracking pattern.

Framework: Backtracking • Interview Prep: Tested at FAANG • Explore all Backtracking problems & invariants →

How to Recognize This Pattern

  • Find word path in grid
  • Mark-and-restore visited cells

Complexity Analysis

Time Complexity

O(m × n × 4^L)

Space Complexity

O(L)

Tags

Array Matrix Backtracking DFS

Frequently Asked Questions

What is the optimal time and space complexity for Word Search?
The optimal solution for Word Search achieves O(m × n × 4^L) runtime complexity and requires O(L) auxiliary space. Time Complexity is O(M * N * 3^L) where M is the number of rows, N is the number of columns, and L is the length of the word. From each character, we explore 3 directions (excluding the direction we arrived from). In the worst case we explore up to 3^L paths per starting cell. Space Complexity is O(L) representing the recursion call stack depth, which never exceeds the length of the word.
How do I identify that Word Search requires the Backtracking pattern?
In technical interviews, look for key problem signals: Find word path in grid; Mark-and-restore visited cells. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Backtracking strategy.
What critical edge cases must be handled in Word Search?
Essential edge cases to verify include: Word length exceeds total grid cells (L > M * N) — return false immediately; Single character word matching board[0][0] — returns true instantly on first step; Word cannot be formed because character frequency on board is insufficient; Self-intersecting paths — temporarily setting board[i][j] = '*' ensures a cell is never reused in the same word instance. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Word Search?
Word Search is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Facebook, Microsoft, Bloomberg.
Buy me a coffee
Daily Practice
0/0