-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfindWords.js
74 lines (57 loc) · 1.42 KB
/
findWords.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const charCodeA = 97
const getCharCodeIndex = (char) => {
return char.charCodeAt(0) - charCodeA
}
const TrieNode = class {
constructor () {
this.next = new Array(26)
this.word = undefined
}
}
const buildTrie = (words) => {
const root = new TrieNode()
for (const word of words) {
let node = root
for (const char of word) {
const idx = getCharCodeIndex(char)
node.next[idx] = node.next[idx] || new TrieNode()
node = node.next[idx]
}
node.word = word
}
return root
}
const dfs = (board, i, j, node, result) => {
const char = board[i][j]
const idx = getCharCodeIndex(char)
if (char === '#' || node.next[idx] === undefined) {
return
}
node = node.next[idx]
if (node.word) {
result.push(node.word)
node.word = undefined
}
board[i][j] = '#'
if (i > 0) dfs(board, i - 1, j, node, result)
if (j > 0) dfs(board, i, j - 1, node, result)
if (i < board.length - 1) dfs(board, i + 1, j, node, result)
if (j < board[0].length - 1) dfs(board, i, j + 1, node, result)
board[i][j] = char
}
/**
* @param {character[][]} board
* @param {string[]} words
* @return {string[]}
*/
const findWords = function (board, words) {
const root = buildTrie(words)
const result = []
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
dfs(board, i, j, root, result)
}
}
return result
}
module.exports = findWords