hardArrayBacktrackingMatrixBacktracking

Word Search

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

Signals to notice

find word in gridadjacent cellsexplore all paths

Brute force first

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.

The key insight

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.

What must stay true

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.

Easy way to go wrong

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