Word Search
Backtracking
Dharun Raju
22z319
Gowtham
22z324
Gowri Sankar
22z325
Table of contents
01 02 03
Problem Approach Sample
Statement Problem
04 05 06
Algorithm Source Code Complexity
(Pseudocode) Analysis
Problem Statement : Word Search(Leetcode)
● 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 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.
Problem Statement : Word Search(Leetcode)
Problem Statement : Word Search(Leetcode)
Example:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
● Given word = "ABCCED", return true.
● Given word = "SEE", return true.
● Given word = "ABCB", return false.
Constraints
● m == board.length
● n = board[i].length
● 1 <= m, n <= 6
● 1 <= word.length <= 15
● board and word consists of only lowercase and uppercase
English letters.
Approach
● Examine the keywords in the problem statement
such as "search" and "adjacent" to understand the
nature of the problem.
● Note the repetition in searching for characters of
the word and finding adjacent elements.
● Think about using recursion since it's suitable for
traversing all elements in the array.
Approach
● Narrow down the options to BFS and DFS. Since
we're traversing deeper, DFS is more suitable.
● Start implementing DFS to recursively search for
the next adjacent element.
● If the current path doesn't lead to the solution,
backtrack and try a different path.
● Determine when to stop the recursion, either when
the entire word is found or when it's impossible to
continue.
Approach
● Ensure constraints such as not reusing cells
and staying within the board bounds are
met.
● Implement optimizations like memoization
if needed.
Pseudocode Algorithm
function DFS(board, word, idx, i, j, rows, cols):
if idx equals length of word:
return true // Base case: entire word found
if i < 0 or i >= rows or j < 0 or j >= cols or board[i][j] not equal to
word[idx]:
return false // Out of bounds or current cell doesn't
match
temp = board[i][j] // Save current character
board[i][j] = '#' // Mark cell as visited
Pseudocode Algorithm
// Explore adjacent cells in DFS order
found = DFS(board, word, idx + 1, i + 1, j, rows, cols) or
DFS(board, word, idx + 1, i - 1, j, rows, cols) or
DFS(board, word, idx + 1, i, j + 1, rows, cols) or
DFS(board, word, idx + 1, i, j - 1, rows, cols)
board[i][j] = temp // Restore the original character
return found
Pseudocode Algorithm
function exist(board, word):
rows = number of rows in board
cols = number of columns in board
for each i from 0 to rows - 1:
for each j from 0 to cols - 1:
if DFS(board, word, 0, i, j, rows, cols) is true:
return true
return false
Source code:
#include <stdio.h>
#include <stdbool.h>
#define ROWS 3
#define COLS 4
bool dfs(char board[ROWS][COLS], char* word, int idx, int i, int j, int rows, int cols) {
if (idx == strlen(word))
return true; // Base case: entire word found
if (i < 0 || i >= rows || j < 0 || j >= cols || board[i][j] != word[idx])
return false; // Out of bounds or current cell doesn't match
char temp = board[i][j]; // Save current character
board[i][j] = '#'; // Mark cell as visited
Source code:
// Explore adjacent cells in DFS order
bool found = dfs(board, word, idx + 1, i + 1, j, rows, cols) ||
dfs(board, word, idx + 1, i - 1, j, rows, cols) ||
dfs(board, word, idx + 1, i, j + 1, rows, cols) ||
dfs(board, word, idx + 1, i, j - 1, rows, cols);
board[i][j] = temp; // Restore the original character
return found;
}
Source code:
bool exist(char board[ROWS][COLS], char* word) {
int rows = ROWS;
int cols = COLS;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (dfs(board, word, 0, i, j, rows, cols))
return true;
}
}
return false;
}
Source code:
int main() {
char board[ROWS][COLS] = {
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'}
};
char* word1 = "ABCCED";
char* word2 = "SEE";
char* word3 = "ABCB"; // Not present
printf("Word1 exists:%s\n",exist(board, word1) ? "true" : "false");
printf("Word2 exists:%s\n",exist(board, word2) ? "true" : "false");
printf("Word3 exists:%s\n",exist(board, word3) ? "true" : "false");
return 0;
}
Time and Space Complexity
Time Complexity:
● The backtracking function explores adjacent cells in a depth-first
manner, potentially making up to 4 recursive calls for each cell.
● As explained, the worst-case scenario creates a 3-ary tree due to the
nature of the exploration (reducing choices to 3 after the initial step)
● With N cells in the board and a word length of L, the total number of
invocations of the backtracking function could be visualized as
approximately 3^L in the worst case.
● Since we iterate through the entire board for backtracking, there
could be up to N invocations of the backtracking function.
● Hence, the overall time complexity is O(N * 3^L).
Time and Space Complexity
Space Complexity:
● The primary memory consumption lies in the recursion call
stack of the backtracking function.
● The maximum depth of recursion would be the length of
the word (L).
● Thus, the space complexity of the algorithm is O(L).
Thank
you !