Skip to content

Commit 70f3ef1

Browse files
committed
refactor: feat: 79. Word Search
1 parent 0cdbba8 commit 70f3ef1

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

word-search/gwbaik9717.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// h: height of the board, w: width of the board, n: length of the word
22
// Time complexity: O(h * w * 4**n)
3-
// Space complexity: O(h * w + n)
3+
// Space complexity: O(n)
44

55
/**
66
* @param {character[][]} board
@@ -15,10 +15,6 @@ var exist = function (board, word) {
1515
const dy = [1, 0, -1, 0];
1616
const dx = [0, 1, 0, -1];
1717

18-
const checked = Array.from({ length: h }, () =>
19-
Array.from({ length: w }, () => 0)
20-
);
21-
2218
let answer = false;
2319

2420
const dfs = (current, index) => {
@@ -28,7 +24,8 @@ var exist = function (board, word) {
2824
}
2925

3026
const [cy, cx] = current;
31-
checked[cy][cx] = 1;
27+
const value = board[cy][cx];
28+
board[cy][cx] = "";
3229

3330
for (let i = 0; i < dy.length; i++) {
3431
const ny = cy + dy[i];
@@ -40,14 +37,14 @@ var exist = function (board, word) {
4037
ny < h &&
4138
nx >= 0 &&
4239
nx < w &&
43-
checked[ny][nx] === 0 &&
40+
board[ny][nx] &&
4441
word[ni] === board[ny][nx]
4542
) {
4643
dfs([ny, nx], ni);
4744
}
4845
}
4946

50-
checked[cy][cx] = 0;
47+
board[cy][cx] = value;
5148
};
5249

5350
for (let i = 0; i < h; i++) {

0 commit comments

Comments
 (0)