Word Search in Grid
Given 2D board and word, find if word exists in grid. Word must be constructed from letters of sequentially adjacent cells (horizontally or vertically). Uses backtracking with visited tracking.
Visualization
Interactive visualization for Word Search in Grid
Word Search (Backtracking)
• Backtracking with DFS
• Explores 4 directions
• Classic grid search problem
Interactive visualization with step-by-step execution
Implementation
1function exist(board: string[][], word: string): boolean {
2 const rows = board.length;
3 const cols = board[0].length;
4
5 function backtrack(row: number, col: number, index: number): boolean {
6 if (index === word.length) return true;
7
8 if (row < 0 || row >= rows || col < 0 || col >= cols ||
9 board[row][col] !== word[index]) {
10 return false;
11 }
12
13 const temp = board[row][col];
14 board[row][col] = '#'; // Mark as visited
15
16 const found = backtrack(row + 1, col, index + 1) ||
17 backtrack(row - 1, col, index + 1) ||
18 backtrack(row, col + 1, index + 1) ||
19 backtrack(row, col - 1, index + 1);
20
21 board[row][col] = temp; // Backtrack
22 return found;
23 }
24
25 for (let i = 0; i < rows; i++) {
26 for (let j = 0; j < cols; j++) {
27 if (backtrack(i, j, 0)) return true;
28 }
29 }
30
31 return false;
32}Deep Dive
Theoretical Foundation
DFS with backtracking from each cell. For each starting position, explore 4 directions recursively. Mark visited cells to avoid cycles, unmark during backtracking. Time: O(N × 3^L) - from each cell, 3 directions (can't go back).
Complexity
Time
O(N)
O(N × 3^L)
O(N × 3^L)
Space
O(L) recursion depth
Applications
Industry Use
Word puzzle games (Boggle, Word Search)
Crossword puzzle solvers
Text pattern matching in 2D layouts
Game development (path finding in grids)
OCR text recognition in structured documents
DNA sequence analysis in 2D representations
Image processing (pattern detection)
Use Cases
Related Algorithms
N-Queens Problem
The N-Queens Problem asks: place N chess queens on an N×N chessboard so that no two queens threaten each other. This means no two queens can be on the same row, column, or diagonal. It's a classic constraint satisfaction problem that demonstrates backtracking, pruning, and systematic exploration of solution spaces. The problem has applications in parallel processing, resource allocation, and algorithm design education.
Sudoku Solver
Sudoku Solver uses backtracking to fill a 9×9 grid with digits 1-9, ensuring each row, column, and 3×3 box contains all digits exactly once. This classic constraint satisfaction problem demonstrates systematic exploration with constraint checking. The algorithm fills empty cells one by one, backtracking when constraints are violated, making it a perfect example of pruning in search algorithms.
Combination Sum
Find all unique combinations in array where chosen numbers sum to target. Numbers can be reused. Classic backtracking problem with pruning optimization.
Generate Valid Parentheses
Generate all combinations of n pairs of well-formed parentheses. Uses backtracking with constraint that closing parentheses never exceed opening ones.