-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path212.word-search-ii.js
69 lines (62 loc) · 1.53 KB
/
212.word-search-ii.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
/*
* @lc app=leetcode id=212 lang=javascript
*
* [212] Word Search II
*/
// @lc code=start
/**
* @param {character[][]} board
* @param {string[]} words
* @return {string[]}
*/
var findWords = function (board, words) {
const trie = new Trie()
for (let word of words) {
trie.insert(word)
}
let ans = new Set()
const traverse = (row, col, nodes, str) => {
if (nodes[board[row][col]].isEnd) {
ans.add(str + board[row][col])
}
nodes = nodes[board[row][col]];
str += board[row][col];
let tmp = board[row][col]
board[row][col] = 1
for (let [newRow, newCol] of [[row + 1, col], [row - 1, col], [row, col + 1], [row, col - 1]]) {
if (
newRow < 0 ||
newCol < 0 ||
newRow >= m ||
newCol >= n ||
!nodes[board[newRow][newCol]] ||
board[newRow][newCol] == 1
) continue;
traverse(newRow, newCol, nodes, str);
}
board[row][col] = tmp
}
let m = board.length, n = board[0].length;
for (let row = 0; row < m; row++) {
for (let col = 0; col < n; col++) {
if (trie.children[board[row][col]]) {
traverse(row, col, trie.children, "");
}
}
}
return [...ans]
};
var Trie = function () {
this.children = {}
}
Trie.prototype.insert = function (word) {
let nodes = this.children
for (let ch of word) {
if (!nodes[ch]) {
nodes[ch] = {}
}
nodes = nodes[ch]
}
nodes.isEnd = true
}
// @lc code=end