Word Search Ii
To solve this coding challenge, we need to identify all the words from the given list that can be found on the provided board of characters. The words can be formed from letters of sequentially adjacent cells, where adjacent cells are those horizontally or vertically neighboring. Each cell cannot be reused within the same word construction.
Explanation
The problem essentially requires a combination of a depth-first search (DFS) algorithm and backtracking. Hereβs a detailed breakdown of the solution:- Initialization and Setup : We need to initialize the board and words list, and setup the necessary variable to keep track of words that are found.
- Depth-First Search (DFS) : For each cell in the board, perform a DFS to explore all possible paths that can form the words from the given list. The DFS will recursively check each adjacent cell (up, down, left, right) while ensuring no cell is reused in the current wordβs construction.
- Backtracking : During the DFS, temporarily mark the current cell as visited by replacing its character with a placeholder. After exploring all paths from the current cell, revert it back to its original state to allow reuse in other word constructions starting from different cells.
- Result Collection : Use a set to collect words that are found during the DFS exploration to avoid duplicates.
- Output : Convert the set of found words into a list and return as the result. Below is the pseudocode with detailed comments on each step:
- Initialization :
-
Create a function
DFS
-
Create a main function
findWords
- Depth-First Search (DFS) :
-
For the
DFS
-
If the conditions are met for being out of boundaries or character mismatch, return
False
-
If all characters of the word have been matched (
length of word == 0
True
-
Temporarily mark the current cell as visited by changing its character to
'#'
-
Recursively call the
DFS
- Restore the current cell's character after exploring all paths from it.
- Backtracking :
- Restore the visited cell back to its original character after exploring all adjacent cells in the current DFS path.
- Result Collection :
- Use a set to store and prevent duplicate words found during the exploration.
- Output :
- Convert the set of found words into a list and return it.
# Pseudocode with Explanation
# Perform Depth-First Search on the board to find the word
Function DFS(board, i, j, word):
# If all letters in the word are successfully matched
If length of word is 0:
Return True
# If out of board boundaries or letter does not match
If i < 0 OR i >= number of rows in board OR j < 0 OR j >= number of columns in board OR board[i][j] != word[0]:
Return False
# Mark the current cell as visited by placing a temporary marker
temp = board[i][j]
board[i][j] = '#'
# Recursively search the adjacent cells (up, down, left, right) for the next letter in the word
result = DFS(board, i + 1, j, word[1:]) OR # Down
DFS(board, i - 1, j, word[1:]) OR # Up
DFS(board, i, j + 1, word[1:]) OR # Right
DFS(board, i, j - 1, word[1:]) # Left
# Revert the current cell to its original character
board[i][j] = temp
Return result
# Main Function to find all words in the board
Function findWords(board, words):
# Initialize a set to store found words to prevent duplicates
found_words = Set()
# Iterate through each word in the words list
For each word in words:
# Iterate through each cell in the board
For i from 0 to number of rows in board:
For j from 0 to number of columns in board:
# If the word is found starting from cell (i, j), add it to the found_words set
If DFS(board, i, j, word):
Add word to found_words
# Convert set of found words to a list and return
Return List(found_words)