Skip to content

Commit 1617422

Browse files
committed
+ problem 79
1 parent 66c579a commit 1617422

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

Problemset/0079-Word Search/README.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# 79. Word Search
2+
Given an `m x n` grid of characters `board` and a string `word`, return `true` *if* `word` *exists in the grid*.
3+
4+
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.
5+
6+
#### Example 1:
7+
![](https://assets.leetcode.com/uploads/2020/11/04/word2.jpg)
8+
<pre>
9+
<strong>Input:</strong> board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
10+
<strong>Output:</strong> true
11+
</pre>
12+
13+
#### Example 2:
14+
![](https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg)
15+
<pre>
16+
<strong>Input:</strong> board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
17+
<strong>Output:</strong> true
18+
</pre>
19+
20+
#### Example 3:
21+
![](https://assets.leetcode.com/uploads/2020/10/15/word3.jpg)
22+
<pre>
23+
<strong>Input:</strong> board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
24+
<strong>Output:</strong> false
25+
</pre>
26+
27+
#### Constraints:
28+
* `m == board.length`
29+
* `n = board[i].length`
30+
* `1 <= m, n <= 6`
31+
* `1 <= word.length <= 15`
32+
* `board` and `word` consists of only lowercase and uppercase English letters.
33+
34+
**Follow up:** Could you use search pruning to make your solution faster with a larger `board`?
35+
36+
## Solutions (Python)
37+
38+
### 1. Solution
39+
```Python
40+
class Solution:
41+
def exist(self, board: List[List[str]], word: str) -> bool:
42+
m, n = len(board), len(board[0])
43+
44+
for r in range(m):
45+
for c in range(n):
46+
if board[r][c] != word[0]:
47+
continue
48+
49+
visited = {(r, c)}
50+
stack = [[r, c, 0, 0]]
51+
52+
while stack != []:
53+
i, j, k, l = stack[-1]
54+
55+
if k == len(word) - 1:
56+
return True
57+
58+
if l == 0:
59+
stack[-1][3] += 1
60+
if i > 0 and (i - 1, j) not in visited and board[i - 1][j] == word[k + 1]:
61+
visited.add((i - 1, j))
62+
stack.append([i - 1, j, k + 1, 0])
63+
elif l == 1:
64+
stack[-1][3] += 1
65+
if i < m - 1 and (i + 1, j) not in visited and board[i + 1][j] == word[k + 1]:
66+
visited.add((i + 1, j))
67+
stack.append([i + 1, j, k + 1, 0])
68+
elif l == 2:
69+
stack[-1][3] += 1
70+
if j > 0 and (i, j - 1) not in visited and board[i][j - 1] == word[k + 1]:
71+
visited.add((i, j - 1))
72+
stack.append([i, j - 1, k + 1, 0])
73+
elif l == 3:
74+
stack[-1][3] += 1
75+
if j < n - 1 and (i, j + 1) not in visited and board[i][j + 1] == word[k + 1]:
76+
visited.add((i, j + 1))
77+
stack.append([i, j + 1, k + 1, 0])
78+
else:
79+
stack.pop()
80+
visited.remove((i, j))
81+
82+
return False
83+
```
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# 79. 单词搜索
2+
给定一个 `m x n` 二维字符网格 `board` 和一个字符串单词 `word` 。如果 `word` 存在于网格中,返回 `true` ;否则,返回 `false`
3+
4+
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
5+
6+
#### 示例 1:
7+
![](https://assets.leetcode.com/uploads/2020/11/04/word2.jpg)
8+
<pre>
9+
<strong>输入:</strong> board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
10+
<strong>输出:</strong> true
11+
</pre>
12+
13+
#### 示例 2:
14+
![](https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg)
15+
<pre>
16+
<strong>输入:</strong> board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
17+
<strong>输出:</strong> true
18+
</pre>
19+
20+
#### 示例 3:
21+
![](https://assets.leetcode.com/uploads/2020/10/15/word3.jpg)
22+
<pre>
23+
<strong>输入:</strong> board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
24+
<strong>输出:</strong> false
25+
</pre>
26+
27+
#### 提示:
28+
* `m == board.length`
29+
* `n = board[i].length`
30+
* `1 <= m, n <= 6`
31+
* `1 <= word.length <= 15`
32+
* `board``word` 仅由大小写英文字母组成
33+
34+
**进阶:**你可以使用搜索剪枝的技术来优化解决方案,使其在 `board` 更大的情况下可以更快解决问题?
35+
36+
## 题解 (Python)
37+
38+
### 1. 题解
39+
```Python
40+
class Solution:
41+
def exist(self, board: List[List[str]], word: str) -> bool:
42+
m, n = len(board), len(board[0])
43+
44+
for r in range(m):
45+
for c in range(n):
46+
if board[r][c] != word[0]:
47+
continue
48+
49+
visited = {(r, c)}
50+
stack = [[r, c, 0, 0]]
51+
52+
while stack != []:
53+
i, j, k, l = stack[-1]
54+
55+
if k == len(word) - 1:
56+
return True
57+
58+
if l == 0:
59+
stack[-1][3] += 1
60+
if i > 0 and (i - 1, j) not in visited and board[i - 1][j] == word[k + 1]:
61+
visited.add((i - 1, j))
62+
stack.append([i - 1, j, k + 1, 0])
63+
elif l == 1:
64+
stack[-1][3] += 1
65+
if i < m - 1 and (i + 1, j) not in visited and board[i + 1][j] == word[k + 1]:
66+
visited.add((i + 1, j))
67+
stack.append([i + 1, j, k + 1, 0])
68+
elif l == 2:
69+
stack[-1][3] += 1
70+
if j > 0 and (i, j - 1) not in visited and board[i][j - 1] == word[k + 1]:
71+
visited.add((i, j - 1))
72+
stack.append([i, j - 1, k + 1, 0])
73+
elif l == 3:
74+
stack[-1][3] += 1
75+
if j < n - 1 and (i, j + 1) not in visited and board[i][j + 1] == word[k + 1]:
76+
visited.add((i, j + 1))
77+
stack.append([i, j + 1, k + 1, 0])
78+
else:
79+
stack.pop()
80+
visited.remove((i, j))
81+
82+
return False
83+
```
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution:
2+
def exist(self, board: List[List[str]], word: str) -> bool:
3+
m, n = len(board), len(board[0])
4+
5+
for r in range(m):
6+
for c in range(n):
7+
if board[r][c] != word[0]:
8+
continue
9+
10+
visited = {(r, c)}
11+
stack = [[r, c, 0, 0]]
12+
13+
while stack != []:
14+
i, j, k, l = stack[-1]
15+
16+
if k == len(word) - 1:
17+
return True
18+
19+
if l == 0:
20+
stack[-1][3] += 1
21+
if i > 0 and (i - 1, j) not in visited and board[i - 1][j] == word[k + 1]:
22+
visited.add((i - 1, j))
23+
stack.append([i - 1, j, k + 1, 0])
24+
elif l == 1:
25+
stack[-1][3] += 1
26+
if i < m - 1 and (i + 1, j) not in visited and board[i + 1][j] == word[k + 1]:
27+
visited.add((i + 1, j))
28+
stack.append([i + 1, j, k + 1, 0])
29+
elif l == 2:
30+
stack[-1][3] += 1
31+
if j > 0 and (i, j - 1) not in visited and board[i][j - 1] == word[k + 1]:
32+
visited.add((i, j - 1))
33+
stack.append([i, j - 1, k + 1, 0])
34+
elif l == 3:
35+
stack[-1][3] += 1
36+
if j < n - 1 and (i, j + 1) not in visited and board[i][j + 1] == word[k + 1]:
37+
visited.add((i, j + 1))
38+
stack.append([i, j + 1, k + 1, 0])
39+
else:
40+
stack.pop()
41+
visited.remove((i, j))
42+
43+
return False

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
[76][76l] |[Minimum Window Substring][76] |![rs]
7979
[77][77l] |[Combinations][77] |![py]
8080
[78][78l] |[Subsets][78] |![rs]
81+
[79][79l] |[Word Search][79] |![py]
8182
[80][80l] |[Remove Duplicates from Sorted Array II][80] |![rs]
8283
[81][81l] |[Search in Rotated Sorted Array II][81] |![rs]
8384
[82][82l] |[Remove Duplicates from Sorted List II][82] |![rb]
@@ -1710,6 +1711,7 @@
17101711
[76]:Problemset/0076-Minimum%20Window%20Substring/README.md#76-minimum-window-substring
17111712
[77]:Problemset/0077-Combinations/README.md#77-combinations
17121713
[78]:Problemset/0078-Subsets/README.md#78-subsets
1714+
[79]:Problemset/0079-Word%20Search/README.md#79-word-search
17131715
[80]:Problemset/0080-Remove%20Duplicates%20from%20Sorted%20Array%20II/README.md#80-remove-duplicates-from-sorted-array-ii
17141716
[81]:Problemset/0081-Search%20in%20Rotated%20Sorted%20Array%20II/README.md#81-search-in-rotated-sorted-array-ii
17151717
[82]:Problemset/0082-Remove%20Duplicates%20from%20Sorted%20List%20II/README.md#82-remove-duplicates-from-sorted-list-ii
@@ -3334,6 +3336,7 @@
33343336
[76l]:https://leetcode.com/problems/minimum-window-substring/
33353337
[77l]:https://leetcode.com/problems/combinations/
33363338
[78l]:https://leetcode.com/problems/subsets/
3339+
[79l]:https://leetcode.com/problems/word-search/
33373340
[80l]:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
33383341
[81l]:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
33393342
[82l]:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
[76][76l] |[最小覆盖子串][76] |![rs]
7979
[77][77l] |[组合][77] |![py]
8080
[78][78l] |[子集][78] |![rs]
81+
[79][79l] |[单词搜索][79] |![py]
8182
[80][80l] |[删除排序数组中的重复项 II][80] |![rs]
8283
[81][81l] |[搜索旋转排序数组 II][81] |![rs]
8384
[82][82l] |[删除排序链表中的重复元素 II][82] |![rb]
@@ -1710,6 +1711,7 @@
17101711
[76]:Problemset/0076-Minimum%20Window%20Substring/README_CN.md#76-最小覆盖子串
17111712
[77]:Problemset/0077-Combinations/README_CN.md#77-组合
17121713
[78]:Problemset/0078-Subsets/README_CN.md#78-子集
1714+
[79]:Problemset/0079-Word%20Search/README_CN.md#79-单词搜索
17131715
[80]:Problemset/0080-Remove%20Duplicates%20from%20Sorted%20Array%20II/README_CN.md#80-删除排序数组中的重复项-ii
17141716
[81]:Problemset/0081-Search%20in%20Rotated%20Sorted%20Array%20II/README_CN.md#81-搜索旋转排序数组-ii
17151717
[82]:Problemset/0082-Remove%20Duplicates%20from%20Sorted%20List%20II/README_CN.md#82-删除排序链表中的重复元素-ii
@@ -3334,6 +3336,7 @@
33343336
[76l]:https://leetcode.cn/problems/minimum-window-substring/
33353337
[77l]:https://leetcode.cn/problems/combinations/
33363338
[78l]:https://leetcode.cn/problems/subsets/
3339+
[79l]:https://leetcode.cn/problems/word-search/
33373340
[80l]:https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/
33383341
[81l]:https://leetcode.cn/problems/search-in-rotated-sorted-array-ii/
33393342
[82l]:https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/

0 commit comments

Comments
 (0)