This approach uses a Trie to represent the list of words for efficient prefix matching. The algorithm performs depth-first search (DFS) on the board starting from each cell. Each path of exploration is checked against the Trie to determine if it forms any of the words or prefixes thereof. The benefit of using a Trie is that we can stop the search early if the current path does not match any prefix in the Trie, thereby improving the efficiency of the search process.
Time Complexity: O(M*(4*3^(L-1))), where M is the number of cells on the board, and L is the maximum length of a word.
Space Complexity: O(N*L), where N is the number of words, and L is the maximum length of a word, used by the Trie structure.
1class TrieNode:
2 def __init__(self):
3 self.children = {}
4 self.word = None
5
6class Solution:
7 def __init__(self):
8 self.result = set()
9
10 def findWords(self, board, words):
11 root = TrieNode()
12 for word in words:
13 node = root
14 for char in word:
15 if char not in node.children:
16 node.children[char] = TrieNode()
17 node = node.children[char]
18 node.word = word
19 for i in range(len(board)):
20 for j in range(len(board[0])):
21 self._dfs(board, i, j, root)
22 return list(self.result)
23
24 def _dfs(self, board, i, j, node):
25 if i < 0 or i >= len(board) or j < 0 or j >= len(board[0]):
26 return
27 char = board[i][j]
28 if char == '#' or char not in node.children:
29 return
30 node = node.children[char]
31 if node.word:
32 self.result.add(node.word)
33 board[i][j] = '#'
34 self._dfs(board, i + 1, j, node)
35 self._dfs(board, i - 1, j, node)
36 self._dfs(board, i, j + 1, node)
37 self._dfs(board, i, j - 1, node)
38 board[i][j] = char
This Python solution constructs a Trie with the input words and explores the board using DFS starting from each cell. It keeps track of words found using a result set, ensuring no duplicates in the output.
In this approach, we perform backtracking for each word individually. This avoids the overhead of constructing a Trie but involves repeated board scanning. A Recursive Depth First Search (DFS) is used to explore possibilities starting from each cell on the board. We maintain a visited matrix to ensure a letter is not reused in forming a particular word.
Time Complexity: O(N * M * 3^L), where N is the number of words, M is the number of cells in the board, and L is the length of the word being searched in the worst case.
Space Complexity: O(L), for the recursive call stack.
1function exist(board, word) {
2 function dfs(i, j, index) {
3 if (index === word.length) return true;
4 if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) return false;
5 if (board[i][j] !== word[index]) return false;
6 let temp = board[i][j];
7 board[i][j] = '#';
8 let found = dfs(i+1, j, index+1) ||
9 dfs(i-1, j, index+1) ||
10 dfs(i, j+1, index+1) ||
11 dfs(i, j-1, index+1);
12 board[i][j] = temp;
13 return found;
14 }
15
16 for (let i = 0; i < board.length; i++) {
17 for (let j = 0; j < board[0].length; j++) {
18 if (dfs(i, j, 0)) return true;
19 }
20 }
21 return false;
22}
This JavaScript solution checks the possibility of forming the word by executing DFS from each cell. By temporarily changing the board values, it prevents reusing the same cell during the formation of a word.