Skip to content

Commit 52aa656

Browse files
committed
💀 [212] Use 79 word search approach TLE
1 parent 022e35a commit 52aa656

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

212/my_solution.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string[]} words
4+
* @return {string[]}
5+
*/
6+
var findWords = function (board, words) {
7+
let foundList = [],
8+
visited = Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => false)),
9+
directions = [
10+
[1, 0],
11+
[0, 1],
12+
[-1, 0],
13+
[0, -1]
14+
]
15+
16+
console.log(board)
17+
console.log(words)
18+
19+
// dfs
20+
const exploreNextWord = (word, i, r, c) => {
21+
console.log(`i:${i},w:${word},r:${r},c:${c}`)
22+
// console.log("visited")
23+
// console.log(visited)
24+
if (word.length === i) return true;
25+
if (
26+
r < 0 || c < 0 ||
27+
r === board.length || c === board[0].length ||
28+
visited[r][c] ||
29+
board[r][c] !== word[i]
30+
) return false;
31+
32+
33+
visited[r][c] = true;
34+
i++;
35+
36+
let res =
37+
exploreNextWord(word, i, r + 1, c) ||
38+
exploreNextWord(word, i, r, c + 1) ||
39+
exploreNextWord(word, i, r - 1, c) ||
40+
exploreNextWord(word, i, r, c - 1);
41+
42+
// console.log(`>>> looking at ${board[r][c]}`)
43+
// if (word[i] === board[r][c]) {
44+
// console.log(`>>> comparing with: ${word[i]} === ${board[r][c]} ?`)
45+
// return true;
46+
// // char.shift();
47+
// // visited = Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => false));
48+
// }
49+
//
50+
// for (let [dr, dc] of directions) {
51+
// if (exploreNextWord(char, i, r + dr, r + dc)) return true;
52+
// }
53+
54+
// Unmark current cell
55+
visited[r][c] = false;
56+
57+
return res;
58+
}
59+
60+
// const visited = [...Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => false))]
61+
// console.log("visited")
62+
// console.log(visited)
63+
for (let i = 0; i < words.length; i++) {
64+
// let tmpVisited = [...visited];
65+
for (let r = 0; r < board.length; r++) {
66+
for (let c = 0; c < board[r].length; c++) {
67+
if (board[r][c] === words[i][0]) {
68+
console.log("____________________________________________")
69+
console.log(`checking:${board[r][c]} === ${words[i][0]}`)
70+
console.log(words[i]);
71+
// console.log(words[i].split("").splice(1, words[i].length - 1))
72+
// if (exploreNextWord(words[i].split("").splice(1, words[i].length - 1), r, c, tmpVisited)) {
73+
// if (exploreNextWord(words[i].split("").splice(1, words[i].length - 1), 0, r, c)) {
74+
if (exploreNextWord(words[i], 0, r, c) && !foundList.includes(words[i])) {
75+
console.log(" FOUND ASKLLDSAKSDLKJADSLJKASDLKJASDKLJS")
76+
foundList.push(words[i]);
77+
}
78+
}
79+
}
80+
}
81+
}
82+
83+
return foundList;
84+
};
85+
86+
/*
87+
Input:
88+
board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]],
89+
words = ["oath","pea","eat","rain"]
90+
Output: ["eat","oath"]
91+
*/
92+
93+
let x =
94+
findWords(
95+
96+
[["o", "a", "b", "n"], ["o", "t", "a", "e"], ["a", "h", "k", "r"], ["a", "f", "l", "v"]],
97+
["oa", "oaa"]
98+
99+
// [["a", "b"], ["c", "d"]],
100+
// ["abcb"]
101+
102+
103+
// [["o", "a", "a", "n"], ["e", "t", "a", "e"], ["i", "h", "k", "r"], ["i", "f", "l", "v"]],
104+
// ["oath", "pea", "eat", "rain"]
105+
)
106+
107+
console.log("result")
108+
console.log(x)

212/solution.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string[]} words
4+
* @return {string[]}
5+
*/
6+
var findWords = function (board, words) {
7+
let foundList = [],
8+
visited = Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => false))
9+
10+
11+
const exploreNextWord = (word, i, r, c) => {
12+
if (word.length === i) return true;
13+
14+
if (
15+
r < 0 || c < 0 ||
16+
r === board.length || c === board[0].length ||
17+
visited[r][c] ||
18+
board[r][c] !== word[i]
19+
) return false;
20+
21+
22+
visited[r][c] = true;
23+
i++;
24+
25+
let res =
26+
exploreNextWord(word, i, r + 1, c) ||
27+
exploreNextWord(word, i, r, c + 1) ||
28+
exploreNextWord(word, i, r - 1, c) ||
29+
exploreNextWord(word, i, r, c - 1);
30+
31+
visited[r][c] = false;
32+
33+
return res;
34+
}
35+
36+
for (let i = 0; i < words.length; i++) {
37+
for (let r = 0; r < board.length; r++) {
38+
for (let c = 0; c < board[r].length; c++) {
39+
if (board[r][c] === words[i][0]) {
40+
if (exploreNextWord(words[i], 0, r, c) && !foundList.includes(words[i])) {
41+
foundList.push(words[i]);
42+
}
43+
}
44+
}
45+
}
46+
}
47+
48+
return foundList;
49+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- [207. Course Schedule](./207/)
6060
- [208. Implement Trie (Prefix Tree)](./209/)
6161
- [211. Design Add and Search Words Data Structure](./211/)
62+
- [212. Word Search II](./212/)
6263
- [213. House Robber II](./213/)
6364
- [217. Contains Duplicate](./217/)
6465
- [226. Invert Binary Tree](./226/)
@@ -150,7 +151,7 @@ Batch create:
150151
NOTE: JS IS HERE
151152
-->
152153
```ssh
153-
chapter=297 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
154+
chapter=212 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
154155
```
155156
> then you can use `x` for quick debug.
156157

0 commit comments

Comments
 (0)