Skip to content

Commit 626f51f

Browse files
committed
💀 [59] Word Search
1 parent 44433a2 commit 626f51f

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

79/my_solution.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
const exist = (board, word) => {
7+
let doesExist = false;
8+
// , wordList = word.split("");
9+
// console.log(wordList);
10+
// wordList.shift()
11+
// console.log(wordList);
12+
13+
const expand = (r, c, visit, wordList) => {
14+
console.log(`r:${r}, c:${c}, visit:`)
15+
console.log(wordList)
16+
console.log(visit)
17+
// console.log(i)
18+
// console.log(word.length)
19+
// console.log(i === word.length)
20+
21+
if (0 === wordList.length) {
22+
console.log(">>>>>>>> >TRUE")
23+
doesExist = true;
24+
}
25+
26+
if (
27+
r === board.length ||
28+
c === board[0].length ||
29+
r < 0 ||
30+
c < 0 ||
31+
1 === visit[r][c]
32+
) {
33+
return;
34+
}
35+
36+
if (wordList[0] === board[r][c]) {
37+
console.log(`${wordList[0]} vs ${board[r][c]}`)
38+
visit[r][c] = 1;
39+
wordList.shift();
40+
41+
expand(r, c + 1, visit, wordList);
42+
expand(r + 1, c, visit, wordList);
43+
expand(r, c - 1, visit, wordList);
44+
expand(r - 1, c, visit, wordList);
45+
}
46+
}
47+
48+
// console.log(board)
49+
for (let r = 0; r < board.length; r++) {
50+
for (let c = 0; c < board[r].length; c++) {
51+
if (!doesExist && word[0] === board[r][c]) {
52+
console.log(`>>>>> ${word[0]} === ${board[r][c]}`)
53+
let visit = Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => 0))
54+
let wordList = word.split("");
55+
expand(r, c, visit, wordList);
56+
}
57+
}
58+
}
59+
60+
// console.log(board)
61+
62+
return doesExist;
63+
};
64+
65+
let x = null;
66+
// x = exist([["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], "ABCCED")
67+
68+
// x = exist([
69+
// ["A", "B", "C", "E"],
70+
// ["S", "F", "C", "S"],
71+
// ["A", "D", "E", "E"]
72+
// ], "ABCCED")//true
73+
74+
// x = exist([
75+
// ["A", "B", "C", "E"],
76+
// ["S", "F", "E", "S"],
77+
// ["A", "D", "E", "E"]
78+
// ], "ABCEFSADEESE") // true
79+
80+
// x = exist(
81+
// [
82+
// ["A", "B", "C", "E"],
83+
// ["S", "F", "C", "S"],
84+
// ["A", "D", "E", "E"]
85+
// ], "ABCB") // false
86+
87+
x = exist(
88+
[
89+
["a", "b"],
90+
["c", "d"]
91+
], "abcd") // false
92+
93+
// x = exist(
94+
// [
95+
// ["a", "a", "a", "a"],
96+
// ["a", "a", "a", "a"],
97+
// ["a", "a", "a", "a"]
98+
// ], "aaaaaaaaaaab") // false
99+
100+
101+
console.log("Result")
102+
console.log(x)

79/solution.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
const exist = (board, word) => {
7+
let doesExist = false;
8+
9+
const expand = (r, c, visit, wordList) => {
10+
if (0 === wordList.length) {
11+
doesExist = true;
12+
}
13+
14+
if (
15+
r === board.length ||
16+
c === board[0].length ||
17+
r < 0 ||
18+
c < 0 ||
19+
1 === visit[r][c]
20+
) {
21+
return;
22+
}
23+
24+
if (wordList[0] === board[r][c]) {
25+
visit[r][c] = 1;
26+
wordList.shift();
27+
28+
expand(r, c + 1, visit, wordList);
29+
expand(r + 1, c, visit, wordList);
30+
expand(r, c - 1, visit, wordList);
31+
expand(r - 1, c, visit, wordList);
32+
}
33+
}
34+
35+
for (let r = 0; r < board.length; r++) {
36+
for (let c = 0; c < board[r].length; c++) {
37+
if (!doesExist && word[0] === board[r][c]) {
38+
let visit = Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => 0))
39+
let wordList = word.split("");
40+
expand(r, c, visit, wordList);
41+
}
42+
}
43+
}
44+
45+
return doesExist;
46+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- [57. Insert Interval](./57/)
3434
- [73. Set Matrix Zeroes](./73/)
3535
- [76. Minimum Window Substring](./76/)
36+
- [79. Word Search](./79/)
3637
- [83. Remove Duplicates from Sorted List](./83/)
3738
- [121. Best Time to Buy and Sell Stock](./121/)
3839
- [128. Longest Consecutive Sequence](./128/)
@@ -119,7 +120,7 @@ Batch create:
119120
NOTE: JS IS HERE
120121
-->
121122
```ssh
122-
chapter=2090 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
123+
chapter=79 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
123124
```
124125
> then you can use `x` for quick debug.
125126

0 commit comments

Comments
 (0)