15% off all plans & roadmaps Use code
See plansLeetCode 79 · Solution & editorial
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
Word Search is a Backtracking Patterns problem, in the Word Search / Path Finding in Grid subpattern. The pattern covers subsets, permutations, combinations, N-Queens, and word search
Search for words in grid using backtracking. Handle visited cells and path restoration.
The reference solution runs in O(m * n * 4^L) time and O(m * n + L) space.
Word Search has been reported in interviews at Amazon, Bloomberg, Intuit, Microsoft, Facebook, Snapchat, Apple, Oracle.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Word Search is a Backtracking Patterns problem, in the Word Search / Path Finding in Grid subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.