forked from harsh432004/LeetCode-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findWords.js
50 lines (42 loc) · 1.05 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
/**
* @param {character[][]} board
* @param {string[]} words
* @return {string[]}
*/
var findWords = function(board, words) {
let result = []
let root = buildTrie(words)
for(let i = 0; i< board.length ;i++){
for(let j=0;j<board[0].length;j++){
dfs(root,i,j,result,board)
}
}
return result
};
function dfs(node,i,j,result,board){
if(node.word){
result.push(node.word)
node.word = null
}
if(i<0 || j<0 || i > board.length -1 || j > board[0].length-1) return
if(!node[board[i][j]]) return
let c = board[i][j]
board[i][j] = '#'
dfs(node[c],i+1,j,result,board)
dfs(node[c],i-1,j,result,board)
dfs(node[c],i,j+1,result,board)
dfs(node[c],i,j-1,result,board)
board[i][j] = c
}
function buildTrie(words){
let root = {}
for(let word of words){
let currNode = root
for(let char of word){
if(!currNode[char]) currNode[char] = {}
currNode = currNode[char]
}
currNode.word = word
}
return root
}