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 constructor() {
3 this.children = new Map();
4 this.word = null;
5 }
6}
7
8function insertWord(root, word) {
9 let node = root;
10 for (const char of word) {
11 if (!node.children.has(char)) {
12 node.children.set(char, new TrieNode());
13 }
14 node = node.children.get(char);
15 }
16 node.word = word;
17}
18
19function findWords(board, words) {
20 const root = new TrieNode();
21 for (const word of words) insertWord(root, word);
22 const result = new Set();
23
24 const dfs = (i, j, node) => {
25 if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) return;
26
27 const char = board[i][j];
28 if (char === '#' || !node.children.has(char)) return;
29
30 node = node.children.get(char);
31 if (node.word) {
32 result.add(node.word);
33 node.word = null; // Avoid duplicates
34 }
35
36 board[i][j] = '#';
37 dfs(i + 1, j, node);
38 dfs(i - 1, j, node);
39 dfs(i, j + 1, node);
40 dfs(i, j - 1, node);
41 board[i][j] = char;
42 };
43
44 for (let i = 0; i < board.length; i++) {
45 for (let j = 0; j < board[0].length; j++) {
46 dfs(i, j, root);
47 }
48 }
49
50 return Array.from(result);
51}
The JavaScript solution uses a Trie to store the words and performs DFS on the board. It checks each cell, follows paths in the Trie as per the board's characters, and stores found words in a Set to avoid duplicates.
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.
1import java.util.HashSet;
2import java.util.Set;
3
4public class Solution {
5 public boolean exist(char[][] board, String word) {
6 for (int i = 0; i < board.length; i++) {
7 for (int j = 0; j < board[i].length; j++) {
8 if (dfs(board, word, i, j, 0, new boolean[board.length][board[0].length])) {
9 return true;
10 }
11 }
12 }
13 return false;
14 }
15
16 private boolean dfs(char[][] board, String word, int i, int j, int index, boolean[][] visited) {
17 if (index == word.length()) return true;
18 if (i < 0 || i >= board.length || j < 0 || j >= board[i].length) return false;
19 if (visited[i][j] || board[i][j] != word.charAt(index)) return false;
20
21 visited[i][j] = true;
22 if (dfs(board, word, i + 1, j, index + 1, visited) ||
23 dfs(board, word, i - 1, j, index + 1, visited) ||
24 dfs(board, word, i, j + 1, index + 1, visited) ||
25 dfs(board, word, i, j - 1, index + 1, visited)) {
26 return true;
27 }
28 visited[i][j] = false;
29 return false;
30 }
31}
This Java solution uses recursive DFS to attempt the formation of each word. A boolean matrix visited
marks positions as used, allowing backtracking if a path becomes invalid. Each possible direction is explored recursively.