forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
59 lines (56 loc) · 1.56 KB
/
Solution.ts
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
class Trie {
children: Trie[];
ref: number;
constructor() {
this.children = new Array(26);
this.ref = -1;
}
insert(w: string, ref: number): void {
let node: Trie = this;
for (let i = 0; i < w.length; i++) {
const c = w.charCodeAt(i) - 97;
if (node.children[c] == null) {
node.children[c] = new Trie();
}
node = node.children[c];
}
node.ref = ref;
}
}
function findWords(board: string[][], words: string[]): string[] {
const tree = new Trie();
for (let i = 0; i < words.length; ++i) {
tree.insert(words[i], i);
}
const m = board.length;
const n = board[0].length;
const ans: string[] = [];
const dirs: number[] = [-1, 0, 1, 0, -1];
const dfs = (node: Trie, i: number, j: number) => {
const idx = board[i][j].charCodeAt(0) - 97;
if (node.children[idx] == null) {
return;
}
node = node.children[idx];
if (node.ref != -1) {
ans.push(words[node.ref]);
node.ref = -1;
}
const c = board[i][j];
board[i][j] = '#';
for (let k = 0; k < 4; ++k) {
const x = i + dirs[k];
const y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] != '#') {
dfs(node, x, y);
}
}
board[i][j] = c;
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
dfs(tree, i, j);
}
}
return ans;
}