|
| 1 | +// const words = ["oath", "pea", "eat", "rain"]; |
| 2 | +// const trie = buildTrie(words); |
| 3 | + |
| 4 | +// Trie in JavaScript Object Notation: |
| 5 | +// { |
| 6 | +// "o": { "a": { "t": { "h": { "word": "oath" } } } }, |
| 7 | +// "p": { "e": { "a": { "word": "pea" } } }, |
| 8 | +// "e": { "a": { "t": { "word": "eat" } } }, |
| 9 | +// "r": { "a": { "i": { "n": { "word": "rain" } } } } |
| 10 | +// } |
| 11 | + |
| 12 | +class TrieNode { |
| 13 | + constructor() { |
| 14 | + this.children = {}; // Stores child nodes (next characters) |
| 15 | + this.word = null; // Stores the word when a full word is formed |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +const buildTrie = (words) => { |
| 20 | + let root = new TrieNode(); // Create the root node |
| 21 | + |
| 22 | + for (const word of words) { |
| 23 | + // Iterate over each word |
| 24 | + let node = root; |
| 25 | + |
| 26 | + for (const char of word) { |
| 27 | + // Iterate over each character in the word |
| 28 | + if (!node.children[char]) { |
| 29 | + node.children[char] = new TrieNode(); // Create node if missing |
| 30 | + } |
| 31 | + node = node.children[char]; // Move to the next node |
| 32 | + } |
| 33 | + |
| 34 | + node.word = word; // Store word at the end node |
| 35 | + } |
| 36 | + |
| 37 | + return root; |
| 38 | +}; |
| 39 | + |
| 40 | +// ✅ Time Complexity: O(N * L + M * 4^L) |
| 41 | +// ✅ Space Complexity: O(N * L + M) |
| 42 | + |
| 43 | +// N represents the number of words in the words array. |
| 44 | +// L represents the maximum length of a word in the words array. |
| 45 | +// M represents the total number of cells on the board, which is the product of the number of rows and columns: M = rows × cols |
| 46 | + |
| 47 | +/** |
| 48 | + * @param {character[][]} board |
| 49 | + * @param {string[]} words |
| 50 | + * @return {string[]} |
| 51 | + */ |
| 52 | +var findWords = function (board, words) { |
| 53 | + const root = buildTrie(words); |
| 54 | + const result = new Set(); // To store found words |
| 55 | + const rows = board.length, |
| 56 | + cols = board[0].length; |
| 57 | + |
| 58 | + const dfs = (node, r, c) => { |
| 59 | + if ( |
| 60 | + r < 0 || |
| 61 | + c < 0 || |
| 62 | + r >= rows || |
| 63 | + c >= cols || |
| 64 | + !node.children[board[r][c]] |
| 65 | + ) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const char = board[r][c]; |
| 70 | + node = node.children[char]; // Move to the next Trie node |
| 71 | + |
| 72 | + if (node.word) { |
| 73 | + // If a word is found at this node |
| 74 | + result.add(node.word); // Add it to the result set |
| 75 | + node.word = null; // Avoid duplicate results |
| 76 | + } |
| 77 | + |
| 78 | + board[r][c] = "#"; // Temporarily mark visited cell |
| 79 | + |
| 80 | + // Explore all 4 directions |
| 81 | + dfs(node, r + 1, c); |
| 82 | + dfs(node, r - 1, c); |
| 83 | + dfs(node, r, c + 1); |
| 84 | + dfs(node, r, c - 1); |
| 85 | + |
| 86 | + board[r][c] = char; // Restore the original character |
| 87 | + |
| 88 | + // 🔥 Remove node if it has no children (prune Trie) |
| 89 | + if (Object.keys(node.children).length === 0) { |
| 90 | + delete node.children[char]; |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + for (let r = 0; r < rows; r++) { |
| 95 | + for (let c = 0; c < cols; c++) { |
| 96 | + if (root.children[board[r][c]]) { |
| 97 | + dfs(root, r, c); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return Array.from(result); |
| 103 | +}; |
| 104 | + |
0 commit comments