
Problem
Solution
Submissions
Word Search II
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Given an m x n board of characters and a list of strings words, find all words in the board. Each word must 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 in a word.
Example 1
- Input:
board = [
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v'] ]
words = ["oath","pea","eat","rain"] - Output: ["eat","oath"]
- Explanation:
- Step 1: Consider the board and list of words.
- Step 2: For "oath", we can find it by starting at board[0][0] and following the path: 'o' → 'a' → 't' → 'h'.
- Step 3: For "eat", we can find it by starting at board[0][3] and following the path: 'e' → 'a' → 't'.
- Step 4: For "pea" and "rain", we cannot find paths on the board.
- Step 5: Return ["eat","oath"] as the result.
Example 2
- Input:
board = [
['a','b'],
['c','d'] ]
words = ["abcb"] - Output: []
- Explanation:
- Step 1: Consider the board and list of words.
- Step 2: For "abcb", we can find 'a' → 'b' → 'c', but then we need another 'b'.
- Step 3: Since the same letter cell 'b' cannot be used twice, we cannot find "abcb" on the board.
- Step 4: Return an empty list as the result.
Constraints
- m == board.length
- n == board[i].length
- 1 ≤ m, n ≤ 12
- board[i][j] is a lowercase English letter.
- 1 ≤ words.length ≤ 3 * 10^4
- 1 ≤ words[i].length ≤ 10
- words[i] consists of lowercase English letters.
- All the strings of words are unique.
- Time Complexity: O(M * N * 4^L) where M,N are dimensions of board and L is max word length
- Space Complexity: O(sum of word lengths)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a Trie data structure to store and search for words efficiently.
- Implement a DFS traversal of the board to find words.
- Mark visited cells during traversal to prevent reusing them.
- Optimize by pruning paths that cannot form any word in the dictionary.
- Keep track of found words to avoid duplicates.