Problem LeetCode 79. Word Search ↗ · NeetCode ↗· The solutions on this page are my own.

Word Search Visualizer

LeetCode 79, step by step: three versions of the same search next to their Java code. Watch a path grow letter by letter, hit a dead end, and get undone. Edit the board or the word and every version reruns.

The problem, step by step

Skip to the visualizer ↓

Can the word be traced on the letter board?

You get a grid of letters and a word. Start on any cell, then step to a neighboring cell for each next letter. If you can spell the whole word this way, the answer is true.

  1. What you get

    A board of letters with m rows and n columns, and a word.

  2. What you return

    true if the word can be traced on the board, false if it cannot. You do not return the path itself.

  3. The rules

    • Each next letter must be in a cell that shares a side with the previous one: up, down, left or right. Diagonal steps do not count.
    • One path may not use the same cell twice.
    • The path can start on any cell and turn in any direction along the way.
    • Letters must match exactly: C and c are different letters.
  4. Try it by hand

    Input
    the board below, word = "CORE"
    Output
    true
    the path for CORE, numbered
    1. Find every cell that holds the first letter, C. There is one, at row 0, column 0.
    2. Next letter O. From C you can step right to A or down to O. Go down to (1,0).
    3. Next letter R. The neighbors of (1,0) are C (already on this path), D and R. Go right to R at (1,1).
    4. Next letter E. The E at (1,2) is right of R.
    5. All four letters are matched, so the answer is true.

    Two words that fail on the same board:

    • "CRE" is false: the R touches C only at a corner, and diagonal steps are not allowed.
    • "ROR" is false: from R you can reach an O, but the only R on the board is the one you started on, and a path cannot use a cell twice.
  5. Edge cases to check

    • A word longer than the number of cells is always false.
    • A one letter word is true when that letter is anywhere on the board.
    • Dead ends: a path can match several letters and then get stuck. That attempt fails, but another start or another turn can still succeed.
    • Words with a repeated letter, like "ROR", are where reusing a cell sneaks in.
  6. The question behind the solutions

    From each cell the path can branch up to four ways, and a branch may fail only after several letters. How do you try every branch, remember which cells the current path is using, and free them again when you back out?

Three ways to trace the word

The board from above and the word "CORE", sketched the way you would draw it in an interview. All three are the same backtracking search; they differ in where they remember the path and how much work they skip. Then step through them below.

1. Visited array WordSearchVisitedArray

Remembers: a visited grid for the current path, and one call per matched letter.

CATSOREADOGS1234red: tried, wrong lettervisited[][]TTTTdfs(C, 0)dfs(O, 1)dfs(R, 2)dfs(E, 3)call stackall 4 letters matched: true
  1. Start a search from every cell that holds the first letter.
  2. The cell matches the next letter and is not visited? Mark it and try its four neighbors for the letter after.
  3. On the way back out, unmark it, so another path can use it.

The catch: up to 4 × 3L−1 paths from each start, and an m × n grid of booleans just to remember a path of L cells.

2. In place WordSearchInPlace

Remembers: the path inside the board itself, as '#' marks.

on the way in#ATS##EADOGSon the way outCATSOREADOGSE foundrestore R, O, CC, O, R are '#': the pathis in the board itself'#' never matches a letter, so no cell is used twicerestore even on success:the caller gets its board back unchanged
  1. The cell matches? Save its letter and write '#' in its place.
  2. Try the four neighbors for the next letter.
  3. Put the saved letter back before returning, whether the search succeeded or not.

The catch: it writes to the input. Forget one restore and later searches, or the caller, see a broken board.

3. Pruned WordSearchPruned

Remembers: two small letter counts, then the same '#' search.

count the board's letters before any searchlettercountACDEGORST211112121CAKEno K on the board: false, 0 callsOREO ×2 but E ×1: search ERO from the one ECOREall there, C ×1 and E ×1: search as iscounting costs O(m·n + L),far less than one search
  1. Count the letters on the board and in the word. Any letter short? Return false without searching.
  2. If the first letter is commoner than the last, reverse the word: fewer starts.
  3. Run the in-place search.

The catch: the worst case stays the same: a board can hold every letter and still not the word. The checks only make the common cases cheap.

Three ways to solve it, step by step

Algorithm
Preset

0cells visited
0backtracks
0search() calls
0path length
Word
Recursion stackbottom → top · search(index, cell)

searching…

on the current path (number = order) checked, does not match dead end, just un-marked '#' mark found path

lines that run are highlighted

      

Complexity · m × n board, word length L

time, worst case
extra space

All three compared

VersionTimeExtra spaceMarks the path withMissing letter

Related in the Lab BFS and DFS · Number of Islands, another grid walk

© 2026 Arman Ajabkhani · armantik.com · Privacy