Word Search
To solve this coding challenge, you need to implement a function that will check if a word exists within a given grid, where the word can be formed by sequentially adjacent cells. This involves a backtracking approach to explore every possible path by looking at each cell and its neighboring cells recursively.
Step-by-Step Explanation
- Initialization and Validation :
-
First, ensure the input grid (
) and the target word (
board) are valid according to the constraints provided.word - We will initialize a backtracking helper function to search for the word starting from any cell in the grid.
- Helper Function - Backtracking :
-
The backtracking function checks whether the word can be formed starting from a given cell
and position
(i, j)in the word.k -
If
equals the length of the word, it means the entire word has been found, and we should return
k.True -
If the current position
is out of bounds or the character in the grid does not match the expected character in the word, return
(i, j).False - Marking and Exploring :
- Temporarily mark the current cell as visited by changing its value to avoid revisiting it during the search.
-
Explore the surrounding cells (up, down, left, right) by recursively calling the backtracking function with the next character position
.
k+1 - Restore the cell's original value after the exploration to backtrack correctly.
- Iterative Exploration :
-
Iterate over each cell of the grid, and for each cell, call the backtracking function starting with
to search for the word from that cell.
k=0 -
If any of these calls return
, the word exists in the grid and the function should return
True.True - Result :
-
If no starting cell leads to the entire word being formed, return
.
False
Detailed Steps in Pseudocode
# Initialize the main function
function wordSearch(board, word):
# Define the backtracking function
function backtrack(row, col, index):
# If entire word is found
if index == length(word):
return True
# If out of grid bounds or current character does not match the word's character
if row < 0 OR row >= number of rows in board OR col < 0 OR col >= number of columns in board OR board[row][col] != word[index]:
return False
# Temporarily mark the cell as visited
temp = board[row][col]
board[row][col] = '/'
# Explore all four possible directions
found = backtrack(row + 1, col, index + 1) OR backtrack(row - 1, col, index + 1) OR backtrack(row, col + 1, index + 1) OR backtrack(row, col - 1, index + 1)
# Restore the cell's original value after exploring
board[row][col] = temp
return found
# Iterate over each cell in the grid
for each row in range(number of rows):
for each col in range(number of columns):
# Begin the backtrack search from each cell
if backtrack(row, col, 0):
return True
return False
Explanation
-
Function
:
wordSearch -
Calls the
function for each cell in the grid to start the search.
backtrack -
If any call to
returns
backtrack, the word exists in the grid and the function returnsTrue.True -
If no such path is found, it returns
.
False -
Function
:
backtrack -
Checks if the whole word is matched by comparing if
equals the length of the word.
index -
Ensures that the current coordinates
are within bounds and the current cell matches the corresponding character in the word.
(row, col) -
Marks the cell as visited by temporarily setting its value to
to prevent revisiting during this path search.
'/' - Recursively explores in four directions (downwards, upwards, rightwards, leftwards).
- After the exploration, it restores the original value of the cell to ensure the grid remains unaltered for other paths.
-
Returns
if any of the recursive calls match the remaining part of the word.
True