Word Search II
Given an m x n board of characters and a list of words, return all words found in the board. Each word must be constructed from letters of sequentially adjacent cells (horizontal or vertical), and the same cell may not be used more than once per word. Output found words sorted alphabetically.
Example:
oaan,etae,ihkr,iflv oath,pea,eat,rain
eat oath
- The input is split into two parts: a 4x4 grid of characters (
oaan,etae,ihkr,iflv) and a list of words (oath,pea,eat,rain). - We search for each word in the grid, checking for horizontal and vertical sequences of characters that match the word.
- The words
eatandoathare found in the grid:eatcan be formed from the letters 'e', 'a', 't' in the grid, andoathcan be formed from the letters 'o', 'a', 't', 'h' in the grid. - The found words are returned in alphabetical order, resulting in the output:
eat oath
Constraints:
- 1 <= m, n <= 12
- board[i][j] is a lowercase English letter
- 1 <= len(words) <= 3 * 10^4
Background Knowledge
The "Word Search II" problem involves finding all words in a given list that can be constructed from letters of sequentially adjacent cells in a grid. This problem is classified under the topic of Tries, which is a fundamental data structure in computer science. A Trie (also known as a prefix tree) is a tree-like data structure that is often used to store a dynamic set or associative array where the keys are usually strings. In the context of this problem, a Trie can be used to efficiently store and search for words in the given list.
The problem also requires an understanding of graph traversal algorithms, as we need to explore the grid to find words. Specifically, we will use a depth-first search (DFS) approach to traverse the grid and construct words. DFS is a traversal algorithm that explores a graph or tree by visiting a node and then visiting all of its neighbors before backtracking. This approach is particularly useful in this problem, as it allows us to efficiently explore the grid and find words.
In addition to Tries and graph traversal, the problem also requires an understanding of string matching algorithms. We need to be able to efficiently check if a word is present in the grid, which involves matching the characters of the word with the characters in the grid. This can be done using a combination of Trie traversal and string comparison.
Algorithm/Approach
The general approach to solving this problem involves using a Trie to store the given list of words, and then using a DFS algorithm to traverse the grid and find words. The Trie allows us to efficiently store and search for words, while the DFS algorithm enables us to explore the grid and construct words. By combining these two approaches, we can efficiently find all words in the grid.
Step-by-Step Strategy
To implement the solution, follow these steps:
- Create a Trie and insert all words from the given list into the Trie.
- Initialize an empty set to store the found words.
- Define a DFS function that takes a cell in the grid, the current word, and the current node in the Trie as parameters.
- In the DFS function, check if the current cell is within the grid boundaries and if the character at the current cell matches the character at the current node in the Trie.
- If the characters match, mark the current cell as visited and recursively call the DFS function for all adjacent cells.
- If the current node in the Trie is a word node (i.e., it represents the end of a word), add the word to the set of found words.
- After the DFS function returns, unmark the current cell as visited to allow other words to use the same cell.
Common Pitfalls
When implementing the solution, watch out for the following common pitfalls:
- Failing to handle the case where a word is a prefix of another word in the Trie.
- Not properly marking and unmarking cells as visited to avoid using the same cell multiple times in a word.
- Not handling the case where a word is found multiple times in the grid.
Time & Space Complexity
The expected time complexity of the solution is O(Nâ‹…Mâ‹…4Lâ‹…W), where N is the number of rows in the grid, M is the number of columns in the grid, L is the maximum length of a word, and W is the number of words in the given list. The expected space complexity is O(Nâ‹…M+W), where the first term represents the space needed to store the grid and the Trie, and the second term represents the space needed to store the found words.