-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0079-word-search.ts
46 lines (38 loc) · 1.29 KB
/
0079-word-search.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
function exist(board: string[][], word: string): boolean {
const rowsLength = board.length;
const colsLength = board[0].length;
function outOfBounds(r: number, c: number) {
return r < 0 || c < 0 || r >= rowsLength || c >= colsLength;
}
// idx: the index of the current character in the word we're looking for
function dfs(row: number, col: number, idx: number): boolean {
if (idx === word.length) {
return true;
}
if (outOfBounds(row, col) || word[idx] !== board[row][col]) {
return false;
}
// Mark the current cell as visited
let currentCell = board[row][col];
board[row][col] = '*';
// Pass idx + 1 because we're looking for
// the next character in the word now
let result = dfs(row + 1, col, idx + 1) || // down
dfs(row - 1, col, idx + 1) || // up
dfs(row, col + 1, idx + 1) || // right
dfs(row, col - 1, idx + 1); // left
// Reset the current cell to its original value
// because we're done visiting it
board[row][col] = currentCell;
return result;
}
// For each cell, do a depth-first search
for (let i = 0; i < rowsLength; i++) {
for (let j = 0; j < colsLength; j++) {
if (dfs(i, j, 0)) {
return true;
}
}
}
return false;
}