-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution139.java
More file actions
31 lines (27 loc) · 912 Bytes
/
Copy pathSolution139.java
File metadata and controls
31 lines (27 loc) · 912 Bytes
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
package leecode;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* 单词拆分
*/
public class Solution139 {
public static void main(String[] args) {
Solution139 s = new Solution139();
System.out.println(s.wordBreak("leetcode", Arrays.asList("leet", "code")));
System.out.println(s.wordBreak("applepenapple", Arrays.asList("apple", "pen")));
}
public boolean wordBreak(String s, List<String> wordDict) {
HashSet<String> wordDictSet = new HashSet<>(wordDict);
boolean[] dp = new boolean[s.length()];
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
if ((j == 0 || dp[j-1]) && wordDictSet.contains(s.substring(j, i+1))) {
dp[i] = true;
break;
}
}
}
return dp[s.length() - 1];
}
}