MemotivaLeetCode Patterns Flashcards: Backtracking, Decision Trees, Pruning, Permutations

How does the word search problem use backtracking on a grid?

LeetCode Patterns Flashcards: Backtracking, Decision Trees, Pruning, Permutations

Аудио-карточка · 0:25

Nortren·

How does the word search problem use backtracking on a grid?

0:25

Start from each cell matching the first character. From each cell, explore all four directions looking for the next character, marking cells as visited to avoid revisiting. If the path matches the entire word, return true. If the current cell does not match or is already visited, backtrack by unmarking the cell and returning false. The key constraint is that each cell can only be used once per path, enforced by the visited marking. After exploring all paths from a cell, unmark it so other starting points can use it.
neetcode.io