-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString139.java
More file actions
48 lines (46 loc) · 1.31 KB
/
String139.java
File metadata and controls
48 lines (46 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package string;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @ProjectName: leetcode
* @Package: string
* @ClassName: String139
* @Author: markey
* @Description:
* @Date: 2020/3/31 22:39
* @Version: 1.0
*/
public class String139 {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> set = new HashSet<>(wordDict); // 用set会快一点
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && set.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
System.out.println(Arrays.toString(dp));
return dp[s.length()];
}
// 超时
public boolean wordBreak1(String s, List<String> wordDict) {
if (s.isEmpty()) {
return true;
}
for (int j = 0; j < wordDict.size(); j++) {
String prefex = wordDict.get(j);
if (s.length() >= prefex.length() && s.startsWith(prefex)) {
if (wordBreak1(s.substring(prefex.length()), wordDict)) {
return true;
}
}
}
return false;
}
}