-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-search.js
51 lines (43 loc) · 1.45 KB
/
word-search.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
/*
* Given a 2D board and a word, find if the word exists in the grid.
* The word can be constructed from letters of sequentially adjacent cell,
* where "adjacent" cells are those horizontally or vertically neighboring.
* The same letter cell may not be used more than once.
*
* board =
*[
* ['A','B','C','E'],
* ['S','F','C','S'],
* ['A','D','E','E']
* ]
*
* Given word = "ABCCED", return true.
* Given word = "SEE", return true.
* Given word = "ABCB", return false.
*/
const key = (r, c) => `${r}-${c}`;
const isValid = (board, r, c) => board.length > r && r >= 0 &&
board[0].length > c && c >= 0;
const exist = function(board, word) {
const inspect = (board, word, r, c, path = {}) => {
if (!word.length) return true;
if (path[key(r, c)]) return false;
if (!isValid(board, r, c)) return false;
if (board[r][c] !== word[0]) return false;
const nPath = {...path, [key(r, c)]: true};
const nWord = word.slice(1);
return inspect(board, nWord, r+1, c, nPath) ||
inspect(board, nWord, r, c+1, nPath) ||
inspect(board, nWord, r-1, c, nPath) ||
inspect(board, nWord, r, c-1, nPath);
};
for (let r=0; r<board.length; r++) {
for (let c=0; c<board[0].length; c++) {
if (board[r][c] === word[0]) {
if (inspect(board, word, r, c)) return true;
}
}
}
return false;
};
export default exist;