Skip to content
AI360Xpert
Beta
Difficulty: MediumBacktracking

Word Search

Problem in Plain English

Given a grid of letters and a word, decide whether the word can be traced through cells that touch horizontally or vertically without reusing any cell.

Problem Statement

Given an m x n grid of characters board and a string word, return true if word exists in the grid.

The word is built from letters of sequentially adjacent cells that share a horizontal or vertical side, and no cell may be used twice within one traced word.

Constraints

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 6
  • 1 <= word.length <= 15
  • board and word hold only English letters, upper and lower case.

Examples

Example 1

Input
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output
true

Trace A at (0,0) right to B and C, down to C, then down to E and left to D.

Example 2

Input
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output
true

Trace S at (1,3) down to E at (2,3) and left to E at (2,2).

Example 3

Input
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output
false

The second B would need a cell that is already used in the current path.

Example 4

Input
board = [["a"]], word = "A"
Output
false

Matching is case-sensitive. The one lowercase cell does not supply the uppercase A; there is no adjacent alternative.

Intuition

A matching letter is only a possible next step: its neighbors must also supply the remaining letters without reusing a cell. For Example 1, the path A, B, C, C, E, D follows six distinct cells. Keep a record of the cells in the current path, try neighbors in turn, then undo that record on return. Undoing matters even after success because the caller should receive the same board it supplied. Cells used in one failed path may be used in another.

Approaches

DFS With Visited Set

Solution Details

Reveal DFS With Visited Set: intuition, complexity, and code

Hints

Hint 1
Treat each cell as a graph node with only its four side neighbors; the current path must match a prefix of the word.
Hint 2
Mark only cells in the current path, then unmark on return so other paths may use them.
Hint 3
Save the Boolean result before cleanup. A marker outside the legal alphabet lets the board carry the path record without a separate grid.

Edge Cases

  • A word longer than all m times n cells can never fit and should return false at once.
  • A one-cell board matches only a one-letter word equal to that cell.
  • Repeated letters such as ABCB only pass when distinct cells supply each occurrence.

Common Mistakes and Interview Tips

  • Forgetting to restore the stamped cell after the recursive calls, which blocks valid paths that cross it later.
  • Reading board[r][c] before the bounds check, which crashes on edge cells instead of returning false.
  • Reusing one visited record across start cells without clearing it, which hides words that begin later in the scan.
  • Returning immediately after a successful child before restoring the current mark leaves the caller board corrupted.
  • Treating uppercase and lowercase letters as interchangeable or allowing diagonal moves violates the path rules.
  • Claiming the visited approach must branch four ways at every depth: it also forbids returning to its parent, so both searches share the same worst-case exponential bound.

Key Takeaway

Grid path problems become graph searches by treating each cell as a node with four neighbours, and marking plus unmarking in place keeps backtracking memory tight.