Skip to content

Commit 9234616

Browse files
committed
Add JS solution for word search LC medium
1 parent ad523b6 commit 9234616

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
https://leetcode.com/problems/word-search
3+
4+
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
5+
6+
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
7+
8+
Example 1:
9+
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
10+
Output: true
11+
12+
Example 2:
13+
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
14+
Output: true
15+
Example 3:
16+
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
17+
Output: false
18+
19+
Constraints:
20+
m == board.length
21+
n = board[i].length
22+
1 <= m, n <= 6
23+
1 <= word.length <= 15
24+
board and word consists of only lowercase and uppercase English letters.
25+
26+
Follow up: Could you use search pruning to make your solution faster with a larger board?
27+
*/
28+
29+
const wordSearch = (board, word) => {
30+
function dfs(y, x, i, cache = {}) {
31+
if (cache[`${y}${x}`]) {
32+
return false;
33+
}
34+
if (y < 0 || y > board.length - 1 || x < 0 || x > board[0].length - 1) {
35+
return false;
36+
}
37+
if (board[y][x] !== word[i]) {
38+
return false;
39+
}
40+
if (board[y][x] === word[word.length - 1] && i === word.length - 1) {
41+
return true;
42+
}
43+
44+
cache[`${y}${x}`] = true;
45+
46+
const results = [
47+
dfs(y - 1, x, i + 1, { ...cache }),
48+
dfs(y + 1, x, i + 1, { ...cache }),
49+
dfs(y, x - 1, i + 1, { ...cache }),
50+
dfs(y, x + 1, i + 1, { ...cache }),
51+
];
52+
53+
return results.some((result) => result);
54+
}
55+
56+
for (let y = 0; y < board.length; y++) {
57+
for (let x = 0; x < board[y].length; x++) {
58+
if (board[y][x] === word[0]) {
59+
const result = dfs(y, x, 0);
60+
if (result) {
61+
return true;
62+
}
63+
}
64+
}
65+
}
66+
67+
return false;
68+
};
69+
70+
module.exports = { wordSearch };
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { wordSearch } = require("./wordSearch");
2+
3+
describe("wordSearch", () => {
4+
it("should return true if the word is found in the board", () => {
5+
const board = [
6+
["A", "B", "C", "E"],
7+
["S", "F", "C", "S"],
8+
["A", "D", "E", "E"],
9+
];
10+
const word = "ABCCED";
11+
expect(wordSearch(board, word)).toBe(true);
12+
});
13+
14+
it("should return true if the word is found in the board", () => {
15+
const board = [
16+
["A", "B", "C", "E"],
17+
["S", "F", "C", "S"],
18+
["A", "D", "E", "E"],
19+
];
20+
const word = "SEE";
21+
expect(wordSearch(board, word)).toBe(true);
22+
});
23+
24+
it("should return false if the word is not found in the board", () => {
25+
const board = [
26+
["A", "B", "C", "E"],
27+
["S", "F", "C", "S"],
28+
["A", "D", "E", "E"],
29+
];
30+
const word = "ABCB";
31+
expect(wordSearch(board, word)).toBe(false);
32+
});
33+
});

0 commit comments

Comments
 (0)