Opening your learning path
Progress is saved as you study.
Opening your learning path
Progress is saved as you study.
The Word Search / Path Finding in Grid pattern applies when you need the Word Search / Path Finding in Grid technique within the Backtracking Patterns pattern. Its time complexity is O(m × n × 4ᴸ) and space complexity O(L) recursion depth. It is used in 3 problems on Thita, including Check if Word Can Be Placed In Crossword, Word Search and Word Search II. Common variations are N-Queens / Constraint Satisfaction and Parentheses Generation.
Search for words in grid using backtracking. Handle visited cells and path restoration.
Word Search / Path Finding in Grid is one of the 7 subpatterns of the Backtracking Patterns pattern, which covers subsets, permutations, combinations, N-Queens, and word search. The whole pattern is about 3 hours of study. This subpattern is a core technique: expect it to come up directly in interviews.
Master backtracking algorithms including subsets, permutations, combinations, N-Queens, and word search. Exhaustive search with pruning for constraint satisfaction. Problems in this subpattern are usually searched for as word search, grid path finding, backtrack grid, DFS grid, leetcode 79, leetcode 212.
Read the theory for Word Search / Path Finding in Grid, then work the problems attached to it in the browser editor. Your solution runs against the problem's test cases, and the AI coach offers a hint about the technique you are missing rather than a finished solution. Progress is tracked per subpattern, so the Backtracking Patterns tracker shows this one as covered once you have solved its problems.
Word Search / Path Finding in Grid is one lesson in a 16-pattern DSA course. If you are preparing end to end, work the interview-critical patterns first and use the pattern sheet as the checklist; if you are here for one technique, the Backtracking Patterns guide is the shortest path back to the rest of it.
3 problems, easiest first.
Imagine you’re playing a game of Boggle or searching for a hidden word in a crossword puzzle. You start at one letter, and then try moving to adjacent letters—up, down, left, or right—to spell out a word. Sometimes you hit a dead end and have to backtrack, trying a different path. This is the essence of the Word Search / Path Finding in Grid backtracking subpattern.
In programming, this pattern is all about exploring possible paths through a grid (matrix) to find words, sequences, or valid placements. It’s like navigating a maze: you try one direction, and if it doesn’t work, you retrace your steps and try another. This approach is especially useful when:
Everyday analogy: Imagine walking through a city grid, trying to spell out your friend’s name by stepping only on tiles with the right letters, and you can’t step on the same tile twice. If you get stuck, you retrace your steps and try a new route. This is backtracking in action!
Let’s break down the key ideas behind this pattern:
Let’s develop a mental model for tackling these problems:
1A B
2B ALet’s use "Word Search" as our example problem.
Given an
m x ngrid of charactersboardand a stringword, returntrueifwordexists in the grid. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
1#include <vector>
2#include <string>
3using namespace std;
4
5// Helper function for DFS backtracking
6bool dfs(vector<vector<char>>& board, int row, int col, const string& word, int index) {
7 // Base case: all characters matched
8 if (index == word.size()) return true;
9
10 // Check boundaries and character match
11 if (row < 0 || row >= board.size() ||
12 col < 0 || col >= board[0].size() ||
13 board[row][col] != word[index]) {
14 return false;
15 }
16
17 // Temporarily mark the cell as visited
18 char temp = board[row][col];
19 board[row][col] = '#'; // Use a special marker
20
21 // Explore all four directions
22 bool found = dfs(board, row + 1, col, word, index + 1) ||
23 dfs(board, row - 1, col, word, index + 1) ||
24 dfs(board, row, col + 1, word, index + 1) ||
25 dfs(board, row, col - 1, word, index + 1);
26
27 // Unmark the cell (backtrack)
28 board[row][col] = temp;
29
30 return found;
31}
32
33bool exist(vector<vector<char>>& board, string word) {
34 int m = board.size(), n = board[0].size();
35 // Try starting from every cell
36 for (int i = 0; i < m; ++i) {
37 for (int j = 0; j < n; ++j) {
38 if (dfs(board, i, j, word, 0)) {
39 return true; // Found a valid path
40 }
41 }
42 }
43 return false; // No path found for the word
44}Explanation:
dfs function to recursively search for the word.Let’s walk through "Word Search" step by step, as if explaining to a friend:
Intuition:
Imagine you’re looking for the word “CAT” in a letter grid. You can start at any cell and move up, down, left, or right, but never use the same cell twice for the same word. If you hit a dead end, you backtrack and try a new path.
Step-by-Step Approach:
Connection to Pattern:
Edge Cases:
Why This Works:
Complexity:
This approach is powerful for any grid-based word/path search where you need to explore multiple possibilities and backtrack when you hit a dead end.
Signals to Recognize This Pattern:
Common "Aha!" Moments:
Debugging Tips:
Memory Techniques:
With practice, you’ll start to spot these patterns quickly and apply backtracking confidently!