Word Search
Given a 2D grid of characters and a string word, return True if the word exists in the grid. The word can be constructed from sequentially adjacent cells (horizontally or vertically). Each cell may only be used once.
Input: grid rows on separate lines, then the word.
Example:
ABCE SFCS ADEE ABCB
True
- The input grid is searched for the word "ABCCED" (implied, not given in the sample input) in all eight possible directions (up, down, left, right, and four diagonals), but only horizontal and vertical directions are considered in this problem.
- Starting from each cell, the algorithm checks if the current cell matches the first character of the word, then moves to the adjacent cell (horizontally or vertically) to check the next character.
- The word "ABCCED" is found in the grid by starting at the cell containing 'A', then moving right to 'B', down to 'C', down to 'C', down to 'E', and down to 'D'.
- The final output is
Truebecause the word is found in the grid.
Constraints:
- 1 <= m, n <= 6
- 1 <= len(word) <= 15
- board and word consist of English letters
Background Knowledge
The "Word Search" problem falls under the category of Graphs & Search, which involves traversing through a data structure to find a specific pattern or solution. In this case, the data structure is a 2D grid of characters, and the goal is to search for a given word within this grid. The word can be constructed from sequentially adjacent cells, which means that each character in the word must be found in a cell that is directly horizontal or vertical to the previous character. This constraint implies that the search algorithm needs to explore the grid in a way that respects these adjacency relationships.
The concept of adjacency is crucial in this problem, as it defines how cells in the grid are connected to each other. In a 2D grid, each cell has four adjacent cells: one above, one below, one to the left, and one to the right. The search algorithm needs to consider these adjacency relationships when exploring the grid to find the word. Additionally, the problem statement mentions that each cell may only be used once, which means that the algorithm needs to keep track of visited cells to avoid revisiting them and entering an infinite loop.
The "Word Search" problem can be solved using various search algorithms, including Depth-First Search (DFS) and Breadth-First Search (BFS). However, due to the constraint that each cell may only be used once, DFS is a more natural fit for this problem. DFS involves exploring the grid by traversing as far as possible along each branch before backtracking, which allows the algorithm to efficiently search for the word while avoiding revisits to the same cell.
Algorithm/Approach
The general approach to solving the "Word Search" problem involves using a Depth-First Search (DFS) algorithm to traverse the 2D grid and search for the given word. The DFS algorithm will explore the grid by starting at each cell and checking if the current character matches the first character of the word. If it does, the algorithm will recursively explore the adjacent cells to find the remaining characters of the word.
Step-by-Step Strategy
To implement the solution, follow these steps:
- Initialize a 2D grid and a given word as input
- Define a DFS function that takes the current cell coordinates and the current index in the word as parameters
- Within the DFS function, check if the current character in the grid matches the current character in the word
- If it does, recursively call the DFS function on the adjacent cells (up, down, left, right) and increment the index in the word
- Keep track of visited cells to avoid revisiting them
- If the entire word is found, return True; otherwise, return False
Common Pitfalls
When implementing the solution, watch out for the following common pitfalls:
- Failing to keep track of visited cells, leading to infinite loops
- Not checking the boundaries of the grid when exploring adjacent cells
- Not handling the case where the word is not found in the grid
Time & Space Complexity
The expected time complexity of the solution is O(Nâ‹…Mâ‹…4L), where N and M are the dimensions of the grid, and L is the length of the word. The space complexity is O(Nâ‹…M), which is used to store the visited cells. Note that the time complexity is exponential in the length of the word due to the recursive nature of the DFS algorithm.