Skip to content

Commit 5fd6b39

Browse files
committed
+ problem 140
1 parent 23ff413 commit 5fd6b39

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 140. Word Break II
2+
Given a string `s` and a dictionary of strings `wordDict`, add spaces in `s` to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in **any order**.
3+
4+
**Note** that the same word in the dictionary may be reused multiple times in the segmentation.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"]
9+
<strong>Output:</strong> ["cats and dog","cat sand dog"]
10+
</pre>
11+
12+
#### Example 2:
13+
<pre>
14+
<strong>Input:</strong> s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
15+
<strong>Output:</strong> ["pine apple pen apple","pineapple pen apple","pine applepen apple"]
16+
<strong>Explanation:</strong> Note that you are allowed to reuse a dictionary word.
17+
</pre>
18+
19+
#### Example 3:
20+
<pre>
21+
<strong>Input:</strong> s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
22+
<strong>Output:</strong> []
23+
</pre>
24+
25+
#### Constraints:
26+
* `1 <= s.length <= 20`
27+
* `1 <= wordDict.length <= 1000`
28+
* `1 <= wordDict[i].length <= 10`
29+
* `s` and `wordDict[i]` consist of only lowercase English letters.
30+
* All the strings of `wordDict` are **unique**.
31+
* Input is generated in a way that the length of the answer doesn't exceed 10<sup>5</sup>.
32+
33+
## Solutions (Python)
34+
35+
### 1. Solution
36+
```Python
37+
from functools import cache
38+
39+
40+
class Solution:
41+
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
42+
words = set(wordDict)
43+
44+
@cache
45+
def backtracking(s: str) -> Optional[List[str]]:
46+
ret = []
47+
48+
for i in range(1, min(len(s) + 1, 10)):
49+
if s[:i] in wordDict:
50+
if i == len(s):
51+
return ret + [s]
52+
sentences = backtracking(s[i:])
53+
if sentences is not None:
54+
ret.extend("{} {}".format(s[:i], sentence)
55+
for sentence in sentences)
56+
57+
return ret if ret != [] else None
58+
59+
return backtracking(s) if backtracking(s) is not None else []
60+
```
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 140. 单词拆分 II
2+
给定一个字符串 `s` 和一个字符串字典 `wordDict` ,在字符串 `s` 中增加空格来构建一个句子,使得句子中所有的单词都在词典中。**以任意顺序** 返回所有这些可能的句子。
3+
4+
**注意:**词典中的同一个单词可能在分段中被重复使用多次。
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"]
9+
<strong>输出:</strong> ["cats and dog","cat sand dog"]
10+
</pre>
11+
12+
#### 示例 2:
13+
<pre>
14+
<strong>输入:</strong> s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
15+
<strong>输出:</strong> ["pine apple pen apple","pineapple pen apple","pine applepen apple"]
16+
<strong>解释:</strong> 注意你可以重复使用字典中的单词。
17+
</pre>
18+
19+
#### 示例 3:
20+
<pre>
21+
<strong>输入:</strong> s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
22+
<strong>输出:</strong> []
23+
</pre>
24+
25+
#### 提示:
26+
* `1 <= s.length <= 20`
27+
* `1 <= wordDict.length <= 1000`
28+
* `1 <= wordDict[i].length <= 10`
29+
* `s``wordDict[i]` 仅有小写英文字母组成
30+
* `wordDict` 中所有字符串都 **不同**
31+
32+
## 题解 (Python)
33+
34+
### 1. 题解
35+
```Python
36+
from functools import cache
37+
38+
39+
class Solution:
40+
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
41+
words = set(wordDict)
42+
43+
@cache
44+
def backtracking(s: str) -> Optional[List[str]]:
45+
ret = []
46+
47+
for i in range(1, min(len(s) + 1, 10)):
48+
if s[:i] in wordDict:
49+
if i == len(s):
50+
return ret + [s]
51+
sentences = backtracking(s[i:])
52+
if sentences is not None:
53+
ret.extend("{} {}".format(s[:i], sentence)
54+
for sentence in sentences)
55+
56+
return ret if ret != [] else None
57+
58+
return backtracking(s) if backtracking(s) is not None else []
59+
```
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from functools import cache
2+
3+
4+
class Solution:
5+
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
6+
words = set(wordDict)
7+
8+
@cache
9+
def backtracking(s: str) -> Optional[List[str]]:
10+
ret = []
11+
12+
for i in range(1, min(len(s) + 1, 10)):
13+
if s[:i] in wordDict:
14+
if i == len(s):
15+
return ret + [s]
16+
sentences = backtracking(s[i:])
17+
if sentences is not None:
18+
ret.extend("{} {}".format(s[:i], sentence)
19+
for sentence in sentences)
20+
21+
return ret if ret != [] else None
22+
23+
return backtracking(s) if backtracking(s) is not None else []

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
[137][137l] |[Single Number II][137] |![rs]
129129
[138][138l] |[Copy List with Random Pointer][138] |![py]
130130
[139][139l] |[Word Break][139] |![py]&nbsp;&nbsp;![rb]
131+
[140][140l] |[Word Break II][140] |![py]
131132
[141][141l] |[Linked List Cycle][141] |![py]
132133
[142][142l] |[Linked List Cycle II][142] |![py]
133134
[143][143l] |[Reorder List][143] |![py]
@@ -1723,6 +1724,7 @@
17231724
[137]:Problemset/0137-Single%20Number%20II/README.md#137-single-number-ii
17241725
[138]:Problemset/0138-Copy%20List%20with%20Random%20Pointer/README.md#138-copy-list-with-random-pointer
17251726
[139]:Problemset/0139-Word%20Break/README.md#139-word-break
1727+
[140]:Problemset/0140-Word%20Break%20II/README.md#140-word-break-ii
17261728
[141]:Problemset/0141-Linked%20List%20Cycle/README.md#141-linked-list-cycle
17271729
[142]:Problemset/0142-Linked%20List%20Cycle%20II/README.md#142-linked-list-cycle-ii
17281730
[143]:Problemset/0143-Reorder%20List/README.md#143-reorder-list
@@ -3311,6 +3313,7 @@
33113313
[137l]:https://leetcode.com/problems/single-number-ii/
33123314
[138l]:https://leetcode.com/problems/copy-list-with-random-pointer/
33133315
[139l]:https://leetcode.com/problems/word-break/
3316+
[140l]:https://leetcode.com/problems/word-break-ii/
33143317
[141l]:https://leetcode.com/problems/linked-list-cycle/
33153318
[142l]:https://leetcode.com/problems/linked-list-cycle-ii/
33163319
[143l]:https://leetcode.com/problems/reorder-list/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
[137][137l] |[只出现一次的数字 II][137] |![rs]
129129
[138][138l] |[复制带随机指针的链表][138] |![py]
130130
[139][139l] |[单词拆分][139] |![py]&nbsp;&nbsp;![rb]
131+
[140][140l] |[单词拆分 II][140] |![py]
131132
[141][141l] |[环形链表][141] |![py]
132133
[142][142l] |[环形链表 II][142] |![py]
133134
[143][143l] |[重排链表][143] |![py]
@@ -1723,6 +1724,7 @@
17231724
[137]:Problemset/0137-Single%20Number%20II/README_CN.md#137-只出现一次的数字-ii
17241725
[138]:Problemset/0138-Copy%20List%20with%20Random%20Pointer/README_CN.md#138-复制带随机指针的链表
17251726
[139]:Problemset/0139-Word%20Break/README_CN.md#139-单词拆分
1727+
[140]:Problemset/0140-Word%20Break%20II/README_CN.md#140-单词拆分-ii
17261728
[141]:Problemset/0141-Linked%20List%20Cycle/README_CN.md#141-环形链表
17271729
[142]:Problemset/0142-Linked%20List%20Cycle%20II/README_CN.md#142-环形链表-ii
17281730
[143]:Problemset/0143-Reorder%20List/README_CN.md#143-重排链表
@@ -3311,6 +3313,7 @@
33113313
[137l]:https://leetcode.cn/problems/single-number-ii/
33123314
[138l]:https://leetcode.cn/problems/copy-list-with-random-pointer/
33133315
[139l]:https://leetcode.cn/problems/word-break/
3316+
[140l]:https://leetcode.cn/problems/word-break-ii/
33143317
[141l]:https://leetcode.cn/problems/linked-list-cycle/
33153318
[142l]:https://leetcode.cn/problems/linked-list-cycle-ii/
33163319
[143l]:https://leetcode.cn/problems/reorder-list/

0 commit comments

Comments
 (0)