Search⌘ K
AI Features

Solution: Word Search

Explore how to implement a backtracking approach using depth-first search to determine if a word exists in a 2D grid. Understand how to traverse the grid, handle valid cell checks, mark visited cells, and backtrack when needed to explore alternate paths. This lesson helps you develop skills to solve grid-based word search problems with efficient recursion and complexity management.

Statement

Given an m x n grid of characters, board, and a string word, return TRUE if word exists in the grid.

The word can be formed by connecting letters of sequentially adjacent cells. The cells are considered sequentially adjacent when neighbors are either horizontally or vertically neighbors. Each cell can be used only once while forming the word.

Constraints:

  • m ==== board.length

  • n == board[i].length, where 00 \leq i << m

  • 11 \leq m, n ...