Skip to content

Commit 528e3ce

Browse files
committed
Sync LeetCode submission Runtime - 490 ms (47.23%), Memory - 51.7 MB (52.51%)
1 parent ed90ea6 commit 528e3ce

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

0079-word-search/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p>
2+
3+
<p>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.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/word2.jpg" style="width: 322px; height: 242px;" />
8+
<pre>
9+
<strong>Input:</strong> board = [[&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;E&quot;],[&quot;S&quot;,&quot;F&quot;,&quot;C&quot;,&quot;S&quot;],[&quot;A&quot;,&quot;D&quot;,&quot;E&quot;,&quot;E&quot;]], word = &quot;ABCCED&quot;
10+
<strong>Output:</strong> true
11+
</pre>
12+
13+
<p><strong class="example">Example 2:</strong></p>
14+
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg" style="width: 322px; height: 242px;" />
15+
<pre>
16+
<strong>Input:</strong> board = [[&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;E&quot;],[&quot;S&quot;,&quot;F&quot;,&quot;C&quot;,&quot;S&quot;],[&quot;A&quot;,&quot;D&quot;,&quot;E&quot;,&quot;E&quot;]], word = &quot;SEE&quot;
17+
<strong>Output:</strong> true
18+
</pre>
19+
20+
<p><strong class="example">Example 3:</strong></p>
21+
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/word3.jpg" style="width: 322px; height: 242px;" />
22+
<pre>
23+
<strong>Input:</strong> board = [[&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;E&quot;],[&quot;S&quot;,&quot;F&quot;,&quot;C&quot;,&quot;S&quot;],[&quot;A&quot;,&quot;D&quot;,&quot;E&quot;,&quot;E&quot;]], word = &quot;ABCB&quot;
24+
<strong>Output:</strong> false
25+
</pre>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Constraints:</strong></p>
29+
30+
<ul>
31+
<li><code>m == board.length</code></li>
32+
<li><code>n = board[i].length</code></li>
33+
<li><code>1 &lt;= m, n &lt;= 6</code></li>
34+
<li><code>1 &lt;= word.length &lt;= 15</code></li>
35+
<li><code>board</code> and <code>word</code> consists of only lowercase and uppercase English letters.</li>
36+
</ul>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Follow up:</strong> Could you use search pruning to make your solution faster with a larger <code>board</code>?</p>

0079-word-search/solution.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
var exist = function(board, word) {
7+
const mLength = board.length
8+
9+
if(mLength == 0){
10+
return false;
11+
}
12+
13+
const nLength = board[0].length
14+
15+
const directions = [[-1, 0], [0, 1], [1, 0], [0, -1]];
16+
17+
const check = (x,y,k) => {
18+
if (board[x][y] !== word[k]) return false;
19+
if (k === word.length - 1) return true;
20+
21+
board[x][y] = '*'; // mark it as visited
22+
23+
for (const [dx, dy] of directions) {
24+
const i = x + dx;
25+
const j = y + dy;
26+
if (i >= 0 && i < mLength && j >= 0 && j < nLength) {
27+
if (check(i, j, k + 1)) return true;
28+
}
29+
}
30+
31+
board[x][y] = word[k]; // reset the value
32+
return false;
33+
}
34+
35+
for(let i=0;i<mLength;i++){
36+
for(let j=0;j<nLength;j++){
37+
console.log(" I ", i , " J ", j)
38+
if(check(i,j,0)){
39+
return true;
40+
}
41+
}
42+
}
43+
44+
return false;
45+
};

0 commit comments

Comments
 (0)