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.
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.
What you get
A board of letters with m rows and n columns, and a word.
What you return
true if the word can be traced on the board, false if it cannot. You do not return the path itself.
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.
Try it by hand
Input
the board below, word = "CORE"
Output
true
C1ATSO2R3E4ADOGS
the path for CORE, numbered
Find every cell that holds the first letter, C. There is one, at row 0, column 0.
Next letter O. From C you can step right to A or down to O. Go down to (1,0).
Next letter R. The neighbors of (1,0) are C (already on this path), D and R. Go right to R at (1,1).
Next letter E. The E at (1,2) is right of R.
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.
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.
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.
Start a search from every cell that holds the first letter.
The cell matches the next letter and is not visited? Mark it and try its four neighbors for the letter after.
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.
The cell matches? Save its letter and write '#' in its place.
Try the four neighbors for the next letter.
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 letters on the board and in the word. Any letter short? Return false without searching.
If the first letter is commoner than the last, reverse the word: fewer starts.
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
Pruningbefore any search
Recursion stackbottom → top · search(index, cell)
searching…
on the current path (number = order)checked, does not matchdead end, just un-marked'#' markfound path