-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0212-word-search-ii.js
49 lines (44 loc) · 1.27 KB
/
0212-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
/**
* 212. Word Search II
* https://leetcode.com/problems/word-search-ii/
* Difficulty: Hard
*
* Given an m x n board of characters and a list of strings words, return all words on the board.
*
* Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells
* are horizontally or vertically neighboring. The same letter cell may not be used more than once
* in a word.
*/
/**
* @param {character[][]} board
* @param {string[]} words
* @return {string[]}
*/
var findWords = function(board, words) {
const root = {};
const result = new Set();
const m = board.length;
const n = board[0].length;
words.forEach(w => w.split('').reduce((n, c) => n[c] = n[c] || {}, root).word = w);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
dfs(i, j, root);
}
}
return Array.from(result);
function dfs(i, j, node) {
const char = board[i][j];
const next = node[char];
if (!next) return;
if (next.word) {
result.add(next.word);
}
board[i][j] = '#';
for (const [di, dj] of [[0, 1], [1, 0], [0, -1], [-1, 0]]) {
if (i + di >= 0 && i + di < m && j + dj >= 0 && j + dj < n && board[i + di][j + dj] !== '#') {
dfs(i + di, j + dj, next);
}
}
board[i][j] = char;
}
};