Skip to content

Commit 3641bf5

Browse files
committed
+ problem 212
1 parent 9637d3e commit 3641bf5

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 212. Word Search II
2+
Given an `m x n` `board` of characters and a list of strings `words`, return *all words on the board*.
3+
4+
Each word must 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 in a word.
5+
6+
#### Example 1:
7+
![](https://assets.leetcode.com/uploads/2020/11/07/search1.jpg)
8+
<pre>
9+
<strong>Input:</strong> board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
10+
<strong>Output:</strong> ["eat","oath"]
11+
</pre>
12+
13+
#### Example 2:
14+
![](https://assets.leetcode.com/uploads/2020/11/07/search2.jpg)
15+
<pre>
16+
<strong>Input:</strong> board = [["a","b"],["c","d"]], words = ["abcb"]
17+
<strong>Output:</strong> []
18+
</pre>
19+
20+
#### Constraints:
21+
* `m == board.length`
22+
* `n == board[i].length`
23+
* `1 <= m, n <= 12`
24+
* `board[i][j]` is a lowercase English letter.
25+
* <code>1 <= words.length <= 3 * 10<sup>4</sup></code>
26+
* `1 <= words[i].length <= 10`
27+
* `words[i]` consists of lowercase English letters.
28+
* All the strings of `words` are unique.
29+
30+
## Solutions (Python)
31+
32+
### 1. Solution
33+
```Python
34+
class Solution:
35+
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
36+
def dfs(i: int, j: int, root: dict) -> None:
37+
if (i, j) in visited or i < 0 or i >= m or j < 0 or j >= n or board[i][j] not in root:
38+
return None
39+
40+
if root[board[i][j]][0] != "":
41+
ret.append(root[board[i][j]][0])
42+
root[board[i][j]][0] = ""
43+
44+
visited.add((i, j))
45+
dfs(i - 1, j, root[board[i][j]][1])
46+
dfs(i + 1, j, root[board[i][j]][1])
47+
dfs(i, j - 1, root[board[i][j]][1])
48+
dfs(i, j + 1, root[board[i][j]][1])
49+
visited.remove((i, j))
50+
51+
m, n = len(board), len(board[0])
52+
root = {}
53+
visited = set()
54+
ret = []
55+
56+
for word in words:
57+
curr = root
58+
59+
for i, c in enumerate(word):
60+
if c not in curr:
61+
curr[c] = ["", {}]
62+
if i == len(word) - 1:
63+
curr[c][0] = word
64+
curr = curr[c][1]
65+
66+
for i in range(m):
67+
for j in range(n):
68+
dfs(i, j, root)
69+
70+
return ret
71+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 212. 单词搜索 II
2+
给定一个 `m x n` 二维字符网格 `board` 和一个单词(字符串)列表 `words`*返回所有二维网格上的单词*
3+
4+
单词必须按照字母顺序,通过 **相邻的单元格** 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
5+
6+
#### 示例 1:
7+
![](https://assets.leetcode.com/uploads/2020/11/07/search1.jpg)
8+
<pre>
9+
<strong>输入:</strong> board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
10+
<strong>输出:</strong> ["eat","oath"]
11+
</pre>
12+
13+
#### 示例 2:
14+
![](https://assets.leetcode.com/uploads/2020/11/07/search2.jpg)
15+
<pre>
16+
<strong>输入:</strong> board = [["a","b"],["c","d"]], words = ["abcb"]
17+
<strong>输出:</strong> []
18+
</pre>
19+
20+
#### 提示:
21+
* `m == board.length`
22+
* `n == board[i].length`
23+
* `1 <= m, n <= 12`
24+
* `board[i][j]` 是一个小写英文字母
25+
* <code>1 <= words.length <= 3 * 10<sup>4</sup></code>
26+
* `1 <= words[i].length <= 10`
27+
* `words[i]` 由小写英文字母组成
28+
* `words` 中的所有字符串互不相同
29+
30+
## 题解 (Python)
31+
32+
### 1. 题解
33+
```Python
34+
class Solution:
35+
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
36+
def dfs(i: int, j: int, root: dict) -> None:
37+
if (i, j) in visited or i < 0 or i >= m or j < 0 or j >= n or board[i][j] not in root:
38+
return None
39+
40+
if root[board[i][j]][0] != "":
41+
ret.append(root[board[i][j]][0])
42+
root[board[i][j]][0] = ""
43+
44+
visited.add((i, j))
45+
dfs(i - 1, j, root[board[i][j]][1])
46+
dfs(i + 1, j, root[board[i][j]][1])
47+
dfs(i, j - 1, root[board[i][j]][1])
48+
dfs(i, j + 1, root[board[i][j]][1])
49+
visited.remove((i, j))
50+
51+
m, n = len(board), len(board[0])
52+
root = {}
53+
visited = set()
54+
ret = []
55+
56+
for word in words:
57+
curr = root
58+
59+
for i, c in enumerate(word):
60+
if c not in curr:
61+
curr[c] = ["", {}]
62+
if i == len(word) - 1:
63+
curr[c][0] = word
64+
curr = curr[c][1]
65+
66+
for i in range(m):
67+
for j in range(n):
68+
dfs(i, j, root)
69+
70+
return ret
71+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
3+
def dfs(i: int, j: int, root: dict) -> None:
4+
if (i, j) in visited or i < 0 or i >= m or j < 0 or j >= n or board[i][j] not in root:
5+
return None
6+
7+
if root[board[i][j]][0] != "":
8+
ret.append(root[board[i][j]][0])
9+
root[board[i][j]][0] = ""
10+
11+
visited.add((i, j))
12+
dfs(i - 1, j, root[board[i][j]][1])
13+
dfs(i + 1, j, root[board[i][j]][1])
14+
dfs(i, j - 1, root[board[i][j]][1])
15+
dfs(i, j + 1, root[board[i][j]][1])
16+
visited.remove((i, j))
17+
18+
m, n = len(board), len(board[0])
19+
root = {}
20+
visited = set()
21+
ret = []
22+
23+
for word in words:
24+
curr = root
25+
26+
for i, c in enumerate(word):
27+
if c not in curr:
28+
curr[c] = ["", {}]
29+
if i == len(word) - 1:
30+
curr[c][0] = word
31+
curr = curr[c][1]
32+
33+
for i in range(m):
34+
for j in range(n):
35+
dfs(i, j, root)
36+
37+
return ret

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
[209][209l] |[Minimum Size Subarray Sum][209] |![rs]
180180
[210][210l] |[Course Schedule II][210] |![rs]
181181
[211][211l] |[Design Add and Search Words Data Structure][211] |![py]
182+
[212][212l] |[Word Search II][212] |![py]
182183
[213][213l] |[House Robber II][213] |![rs]
183184
[215][215l] |[Kth Largest Element in an Array][215] |![rb]
184185
[216][216l] |[Combination Sum III][216] |![rs]
@@ -1857,6 +1858,7 @@
18571858
[209]:Problemset/0209-Minimum%20Size%20Subarray%20Sum/README.md#209-minimum-size-subarray-sum
18581859
[210]:Problemset/0210-Course%20Schedule%20II/README.md#210-course-schedule-ii
18591860
[211]:Problemset/0211-Design%20Add%20and%20Search%20Words%20Data%20Structure/README.md#211-design-add-and-search-words-data-structure
1861+
[212]:Problemset/0212-Word%20Search%20II/README.md#212-word-search-ii
18601862
[213]:Problemset/0213-House%20Robber%20II/README.md#213-house-robber-ii
18611863
[215]:Problemset/0215-Kth%20Largest%20Element%20in%20an%20Array/README.md#215-kth-largest-element-in-an-array
18621864
[216]:Problemset/0216-Combination%20Sum%20III/README.md#216-combination-sum-iii
@@ -3528,6 +3530,7 @@
35283530
[209l]:https://leetcode.com/problems/minimum-size-subarray-sum/
35293531
[210l]:https://leetcode.com/problems/course-schedule-ii/
35303532
[211l]:https://leetcode.com/problems/add-and-search-word-data-structure-design/
3533+
[212l]:https://leetcode.com/problems/word-search-ii/
35313534
[213l]:https://leetcode.com/problems/house-robber-ii/
35323535
[215l]:https://leetcode.com/problems/kth-largest-element-in-an-array/
35333536
[216l]:https://leetcode.com/problems/combination-sum-iii/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
[209][209l] |[长度最小的子数组][209] |![rs]
180180
[210][210l] |[课程表 II][210] |![rs]
181181
[211][211l] |[添加与搜索单词 - 数据结构设计][211] |![py]
182+
[212][212l] |[单词搜索 II][212] |![py]
182183
[213][213l] |[打家劫舍 II][213] |![rs]
183184
[215][215l] |[数组中的第K个最大元素][215] |![rb]
184185
[216][216l] |[组合总和 III][216] |![rs]
@@ -1857,6 +1858,7 @@
18571858
[209]:Problemset/0209-Minimum%20Size%20Subarray%20Sum/README_CN.md#209-长度最小的子数组
18581859
[210]:Problemset/0210-Course%20Schedule%20II/README_CN.md#210-课程表-ii
18591860
[211]:Problemset/0211-Design%20Add%20and%20Search%20Words%20Data%20Structure/README_CN.md#211-添加与搜索单词---数据结构设计
1861+
[212]:Problemset/0212-Word%20Search%20II/README_CN.md#212-单词搜索-ii
18601862
[213]:Problemset/0213-House%20Robber%20II/README_CN.md#213-打家劫舍-ii
18611863
[215]:Problemset/0215-Kth%20Largest%20Element%20in%20an%20Array/README_CN.md#215-数组中的第k个最大元素
18621864
[216]:Problemset/0216-Combination%20Sum%20III/README_CN.md#216-组合总和-iii
@@ -3528,6 +3530,7 @@
35283530
[209l]:https://leetcode.cn/problems/minimum-size-subarray-sum/
35293531
[210l]:https://leetcode.cn/problems/course-schedule-ii/
35303532
[211l]:https://leetcode.cn/problems/add-and-search-word-data-structure-design/
3533+
[212l]:https://leetcode.cn/problems/word-search-ii/
35313534
[213l]:https://leetcode.cn/problems/house-robber-ii/
35323535
[215l]:https://leetcode.cn/problems/kth-largest-element-in-an-array/
35333536
[216l]:https://leetcode.cn/problems/combination-sum-iii/

0 commit comments

Comments
 (0)