hardArrayBacktrackingMatrixBacktracking

Word Search

hardTime: O(m * n * 4^L)Space: O(L)

Recognize the pattern

find word in gridadjacent cellsexplore all paths

Brute force idea

If you approach Word Search in the most literal way possible, you get this: No simpler alternative — you must explore paths. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

Better approach

The real unlock in Word Search comes when you notice this: Backtracking DFS from each cell matching the first letter. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Key invariant

The compass for Word Search is this: Mark cells as visited during exploration, unmark when backtracking. As long as that statement keeps holding, you can trust the steps built on top of it.

Watch out for

The trap in Word Search usually looks like this: Not unvisiting cells after backtracking — you'll miss valid paths that reuse cells in different branches. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Backtracking Pattern