Skip to content

Commit ea7aca5

Browse files
committed
feat: add solutions to lc problem: No.0139. Word Break
1 parent c2944c1 commit ea7aca5

File tree

9 files changed

+286
-39
lines changed

9 files changed

+286
-39
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
- [买卖股票的最佳时机 III](./solution/0100-0199/0123.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20III/README.md)
178178
- [最佳买卖股票时机含冷冻期](./solution/0300-0399/0309.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Cooldown/README.md)
179179
- [买卖股票的最佳时机含手续费](./solution/0700-0799/0714.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/README.md)
180+
- [单词拆分](./solution/0100-0199/0139.Word%20Break/README.md)
180181
- [接雨水](./solution/0000-0099/0042.Trapping%20Rain%20Water/README.md)
181182
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
182183
- [最小路径和](./solution/0000-0099/0064.Minimum%20Path%20Sum/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
171171
- [Best Time to Buy and Sell Stock III](./solution/0100-0199/0123.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20III/README_EN.md)
172172
- [Best Time to Buy and Sell Stock with Cooldown](./solution/0300-0399/0309.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Cooldown/README_EN.md)
173173
- [Best Time to Buy and Sell Stock with Transaction Fee](./solution/0700-0799/0714.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/README_EN.md)
174+
- [Word Break](./solution/0100-0199/0139.Word%20Break/README_EN.md)
174175
- [Trapping Rain Water](./solution/0000-0099/0042.Trapping%20Rain%20Water/README_EN.md)
175176
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)
176177
- [Decode Ways](./solution/0000-0099/0091.Decode%20Ways/README_EN.md)

solution/0100-0199/0139.Word Break/README.md

+106-2
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,131 @@
3636
<strong>输出:</strong> false
3737
</pre>
3838

39-
4039
## 解法
4140

4241
<!-- 这里可写通用的实现逻辑 -->
4342

43+
动态规划法。
44+
45+
`dp[i]` 表示前 i 个字符组成的字符串 `s[0...i-1]` 能否拆分成若干个字典中出现的单词。
46+
4447
<!-- tabs:start -->
4548

4649
### **Python3**
4750

4851
<!-- 这里可写当前语言的特殊实现逻辑 -->
4952

5053
```python
51-
54+
class Solution:
55+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
56+
words = set(wordDict)
57+
n = len(s)
58+
dp = [False] * (n + 1)
59+
dp[0] = True
60+
for i in range(1, n + 1):
61+
for j in range(i):
62+
if dp[j] and s[j:i] in words:
63+
dp[i] = True
64+
break
65+
return dp[n]
5266
```
5367

5468
### **Java**
5569

5670
<!-- 这里可写当前语言的特殊实现逻辑 -->
5771

5872
```java
73+
class Solution {
74+
public boolean wordBreak(String s, List<String> wordDict) {
75+
Set<String> words = new HashSet<>(wordDict);
76+
int n = s.length();
77+
boolean[] dp = new boolean[n + 1];
78+
dp[0] = true;
79+
for (int i = 1; i <= n; ++i) {
80+
for (int j = 0; j < i; ++j) {
81+
if (dp[j] && words.contains(s.substring(j, i))) {
82+
dp[i] = true;
83+
break;
84+
}
85+
}
86+
}
87+
return dp[n];
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
bool wordBreak(string s, vector<string>& wordDict) {
98+
unordered_set<string> words;
99+
for (auto word : wordDict) {
100+
words.insert(word);
101+
}
102+
int n = s.size();
103+
vector<bool> dp(n + 1, false);
104+
dp[0] = true;
105+
for (int i = 1; i <= n; ++i) {
106+
for (int j = 0; j < i; ++j) {
107+
if (dp[j] && words.find(s.substr(j, i - j)) != words.end()) {
108+
dp[i] = true;
109+
break;
110+
}
111+
}
112+
}
113+
return dp[n];
114+
}
115+
};
116+
```
117+
118+
### **Go**
119+
120+
```go
121+
func wordBreak(s string, wordDict []string) bool {
122+
words := make(map[string]bool)
123+
for _, word := range wordDict {
124+
words[word] = true
125+
}
126+
n := len(s)
127+
dp := make([]bool, n+1)
128+
dp[0] = true
129+
for i := 1; i <= n; i++ {
130+
for j := 0; j < i; j++ {
131+
if dp[j] && words[s[j:i]] {
132+
dp[i] = true
133+
break
134+
}
135+
}
136+
}
137+
return dp[n]
138+
}
139+
```
59140

141+
### **C#**
142+
143+
```cs
144+
public class Solution {
145+
public bool WordBreak(string s, IList<string> wordDict) {
146+
var words = new HashSet<string>(wordDict);
147+
int n = s.Length;
148+
var dp = new bool[n + 1];
149+
dp[0] = true;
150+
for (int i = 1; i <= n; ++i)
151+
{
152+
for (int j = 0; j < i; ++j)
153+
{
154+
if (dp[j] && words.Contains(s.Substring(j, i - j)))
155+
{
156+
dp[i] = true;
157+
break;
158+
}
159+
}
160+
}
161+
return dp[n];
162+
}
163+
}
60164
```
61165

62166
### **...**

solution/0100-0199/0139.Word Break/README_EN.md

+104-1
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,121 @@ Note that you are allowed to reuse a dictionary word.
4747

4848
## Solutions
4949

50+
Dynamic Programming.
51+
5052
<!-- tabs:start -->
5153

5254
### **Python3**
5355

5456
```python
55-
57+
class Solution:
58+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
59+
words = set(wordDict)
60+
n = len(s)
61+
dp = [False] * (n + 1)
62+
dp[0] = True
63+
for i in range(1, n + 1):
64+
for j in range(i):
65+
if dp[j] and s[j:i] in words:
66+
dp[i] = True
67+
break
68+
return dp[n]
5669
```
5770

5871
### **Java**
5972

6073
```java
74+
class Solution {
75+
public boolean wordBreak(String s, List<String> wordDict) {
76+
Set<String> words = new HashSet<>(wordDict);
77+
int n = s.length();
78+
boolean[] dp = new boolean[n + 1];
79+
dp[0] = true;
80+
for (int i = 1; i <= n; ++i) {
81+
for (int j = 0; j < i; ++j) {
82+
if (dp[j] && words.contains(s.substring(j, i))) {
83+
dp[i] = true;
84+
break;
85+
}
86+
}
87+
}
88+
return dp[n];
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
bool wordBreak(string s, vector<string>& wordDict) {
99+
unordered_set<string> words;
100+
for (auto word : wordDict) {
101+
words.insert(word);
102+
}
103+
int n = s.size();
104+
vector<bool> dp(n + 1, false);
105+
dp[0] = true;
106+
for (int i = 1; i <= n; ++i) {
107+
for (int j = 0; j < i; ++j) {
108+
if (dp[j] && words.find(s.substr(j, i - j)) != words.end()) {
109+
dp[i] = true;
110+
break;
111+
}
112+
}
113+
}
114+
return dp[n];
115+
}
116+
};
117+
```
118+
119+
### **Go**
120+
121+
```go
122+
func wordBreak(s string, wordDict []string) bool {
123+
words := make(map[string]bool)
124+
for _, word := range wordDict {
125+
words[word] = true
126+
}
127+
n := len(s)
128+
dp := make([]bool, n+1)
129+
dp[0] = true
130+
for i := 1; i <= n; i++ {
131+
for j := 0; j < i; j++ {
132+
if dp[j] && words[s[j:i]] {
133+
dp[i] = true
134+
break
135+
}
136+
}
137+
}
138+
return dp[n]
139+
}
140+
```
61141

142+
### **C#**
143+
144+
```cs
145+
public class Solution {
146+
public bool WordBreak(string s, IList<string> wordDict) {
147+
var words = new HashSet<string>(wordDict);
148+
int n = s.Length;
149+
var dp = new bool[n + 1];
150+
dp[0] = true;
151+
for (int i = 1; i <= n; ++i)
152+
{
153+
for (int j = 0; j < i; ++j)
154+
{
155+
if (dp[j] && words.Contains(s.Substring(j, i - j)))
156+
{
157+
dp[i] = true;
158+
break;
159+
}
160+
}
161+
}
162+
return dp[n];
163+
}
164+
}
62165
```
63166

64167
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool wordBreak(string s, vector<string>& wordDict) {
4+
unordered_set<string> words;
5+
for (auto word : wordDict) {
6+
words.insert(word);
7+
}
8+
int n = s.size();
9+
vector<bool> dp(n + 1, false);
10+
dp[0] = true;
11+
for (int i = 1; i <= n; ++i) {
12+
for (int j = 0; j < i; ++j) {
13+
if (dp[j] && words.find(s.substr(j, i - j)) != words.end()) {
14+
dp[i] = true;
15+
break;
16+
}
17+
}
18+
}
19+
return dp[n];
20+
}
21+
};
+10-22
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
41
public class Solution {
52
public bool WordBreak(string s, IList<string> wordDict) {
6-
var f = new bool[s.Length + 1];
7-
f[0] = true;
8-
var wordDictGroup = wordDict.GroupBy(word => word.Length);
9-
for (var i = 1; i <= s.Length; ++i)
10-
{
11-
foreach (var wordGroup in wordDictGroup)
12-
{
13-
var wordLength = wordGroup.Key;
14-
if (i >= wordLength && f[i - wordLength])
15-
{
16-
foreach (var word in wordGroup)
17-
{
18-
if (s.Substring(i - wordLength, wordLength) == word)
19-
{
20-
f[i] = true;
21-
break;
22-
}
23-
}
3+
var words = new HashSet<string>(wordDict);
4+
int n = s.Length;
5+
var dp = new bool[n + 1];
6+
dp[0] = true;
7+
for (int i = 1; i <= n; ++i) {
8+
for (int j = 0; j < i; ++j) {
9+
if (dp[j] && words.Contains(s.Substring(j, i))) {
10+
dp[i] = true;
11+
break;
2412
}
2513
}
2614
}
27-
return f[s.Length];
15+
return dp[n];
2816
}
2917
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func wordBreak(s string, wordDict []string) bool {
2+
words := make(map[string]bool)
3+
for _, word := range wordDict {
4+
words[word] = true
5+
}
6+
n := len(s)
7+
dp := make([]bool, n+1)
8+
dp[0] = true
9+
for i := 1; i <= n; i++ {
10+
for j := 0; j < i; j++ {
11+
if dp[j] && words[s[j:i]] {
12+
dp[i] = true
13+
break
14+
}
15+
}
16+
}
17+
return dp[n]
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
class Solution {
2-
public boolean wordBreak(String s, List<String> wordDict) {
3-
if (s == null || s.length() == 0) {
4-
return false;
5-
}
6-
Set<String> words = new HashSet<>(wordDict);
7-
boolean[] dp = new boolean[s.length() + 1];
1+
public class Solution {
2+
public bool WordBreak(string s, IList<string> wordDict) {
3+
var words = new HashSet<string>(wordDict);
4+
int n = s.Length;
5+
var dp = new bool[n + 1];
86
dp[0] = true;
9-
10-
for (int i = 1; i <= s.length(); i++) {
11-
for (int j = i - 1; j > -1; j--) {
12-
dp[i] = dp[j] && words.contains(s.substring(j, i));
13-
if (dp[i]) {
7+
for (int i = 1; i <= n; ++i)
8+
{
9+
for (int j = 0; j < i; ++j)
10+
{
11+
if (dp[j] && words.Contains(s.Substring(j, i - j)))
12+
{
13+
dp[i] = true;
1414
break;
1515
}
1616
}
1717
}
18-
19-
return dp[s.length()];
18+
return dp[n];
2019
}
2120
}

0 commit comments

Comments
 (0)