-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-search.js
48 lines (34 loc) · 1.09 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
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
var exist = function(board, word) {
var len1 = board.length;
var len2 = (board[0] || []).length;
var len3 = word.length;
var visited = null;
if (!len1 || !len2 || !len3) return false;
for (var i = 0; i < len1; i++) {
for (var j = 0; j < len2; j++) {
visited = Array(len1).fill(0).map(_ => Array(len2));
if (helper(board, word, i, j, 0, visited)) return true;
}
}
return false;
};
var helper = function (board, word, m, n, k, visited) {
if (k === word.length) return true;
if (m < 0 || m >= board.length) return false;
if (n < 0 || n >= board[m].length) return false;
if (visited[m][n]) return false;
if (board[m][n] !== word[k]) return false;
var res = false;
visited[m][n] = true;
res = helper(board, word, m - 1, n, k + 1, visited)
|| helper(board, word, m + 1, n, k + 1, visited)
|| helper(board, word, m, n - 1, k + 1, visited)
|| helper(board, word, m, n + 1, k + 1, visited);
if (!res) visited[m][n] = false;
return res;
};