-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboggle-board.js
More file actions
60 lines (55 loc) · 1.88 KB
/
boggle-board.js
File metadata and controls
60 lines (55 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Time O (ws + nm*8^s) | Space O(nm + ws)
class Trie {
constructor() {
this.root = {};
this.endSymbol = "*";
}
add(string) {
let currentNode = this.root;
for (const char of string) {
if (!currentNode.hasOwnProperty(char)) currentNode[char] = {};
currentNode = currentNode[char];
}
currentNode[this.endSymbol] = string;
}
}
function boggleBoard(board, words) {
const finalWords = {};
const visited = board.map((row) => row.map((letter) => false));
const trie = new Trie();
for (const word of words) {
trie.add(word);
}
for (let row = 0; row < board.length; row++) {
for (let col = 0; col < board[0].length; col++) {
explore(row, col, board, visited, trie.root, finalWords);
}
}
return Object.keys(finalWords);
}
function explore(row, col, board, visited, trieNode, finalWords) {
if (visited[row][col]) return;
const letter = board[row][col];
if (!trieNode.hasOwnProperty(letter)) return;
visited[row][col] = true;
trieNode = trieNode[letter];
if ("*" in trieNode) finalWords[trieNode["*"]] = true;
const neighbors = getNeighbors(row, col, board);
for (const neighbor of neighbors) {
explore(neighbor[0], neighbor[1], board, visited, trieNode, finalWords);
}
visited[row][col] = false;
}
function getNeighbors(row, col, board) {
const neighbors = [];
if (row > 0) neighbors.push([row - 1, col]);
if (col > 0) neighbors.push([row, col - 1]);
if (row + 1 < board.length) neighbors.push([row + 1, col]);
if (col + 1 < board[0].length) neighbors.push([row, col + 1]);
if (row > 0 && col > 0) neighbors.push([row - 1, col - 1]);
if (row + 1 < board.length && col > 0) neighbors.push([row + 1, col - 1]);
if (row + 1 < board.length && col + 1 < board[0].length)
neighbors.push([row + 1, col + 1]);
if (row > 0 && col + 1 < board[0].length) neighbors.push([row - 1, col + 1]);
return neighbors;
}