This lesson reinforces Backtracking & Subsets.
Problem in simple words
Given a grid of letters and a word, decide whether the word can be traced through a chain of adjacent cells without reusing any cell.
Core idea
A path can reuse the board shape but not the same cell within one attempt.
How the algorithm thinks
- 1
Try starting the word at each board cell
VISITA matching first letter anywhere on the board is a candidate starting point worth exploring further.
Watch: The cell currently being checked as a start.
- 2
Mark the cell and branch to its neighbors
CALLOnce a cell matches the needed letter, it's marked as used and every unused neighboring direction is tried for the next letter.
Watch: The cell being marked, and the four directions being tried.
- 3
Unmark and back out when no neighbor works
BACKTRACKIf none of the neighbors continue the word, this cell must be freed up again so a different path can still use it.
Watch: The cell being unmarked.
- 4
Return whether any path completed the word
RETURNIf any branch of the search matched every character of the word, the word exists on the board.
Watch: The final true or false answer.
Step-by-step logic
- -For each cell on the board, try starting the word search there.
- -At each step, check whether the current index has reached the word's length (success), whether the cell is off the board or already used or doesn't match the needed letter (failure).
- -If the cell matches, mark it as used and recursively try all four neighboring directions for the next letter.
- -Whether or not a direction succeeds, unmark the current cell once its neighbors have all been tried, so the cell is available again for a different path.
Complexity
Time O(mn*4^L)·Space O(L)
each start can branch through up to four neighbors for L word characters.
Implementation
This is the JavaScript implementation that generates the simulator timeline.
function exist(input) {
for each start cell, dfs with a visited set;
backtrack when a branch fails;
}Common mistakes
Pause before the next step and name the state change you expect.
Watch the active pointers, discarded range, and output state and match it to the highlighted code line.
Say the rule that never breaks (the invariant) out loud: what remains true before and after this step?
- -Step 1: (0,0)='A' matches word[0]='A' -- mark it used.
- -Step 2: down (1,0)='S' fails to match 'B'; up is off the board; right (0,1)='B' matches word[1] -- mark it used.
- -Step 3: down (1,1)='F' fails; up is off the board; right (0,2)='C' matches word[2] -- mark it used. Down from there, (1,2)='C' matches word[3] -- mark it used. Down again, (2,2)='E' matches word[4] -- mark it used.
- -Step 4: from (2,2), down is off the board, up is already used, right (2,3)='E' fails to match 'D', left (2,1)='D' matches word[5] -- the word is complete.
- - What should this function promise to do?
- - Where does the recursion stop?
- - Which smaller problem can you trust?
- - How does current work connect with the smaller answer?
- - List the choices at each step.
- - Reject invalid choices early.
- - Explore one choice completely.
- - Undo the choice before trying the next branch.
- - Choose queue for levels or stack/recursion for depth.
- - Mark visited at the right time.
- - Track frontier, traversal order, and parent/dependency state.
- - Decide what each pointer represents.
- - Move the pointer that can safely eliminate work.
- - Keep the invariant true after every move.
Build the reasoning before opening the simulator
Turn the mental model into a short decision checklist, then compare it with the execution timeline.