-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ140_WordBreakII.java
More file actions
59 lines (56 loc) · 1.67 KB
/
Copy pathQ140_WordBreakII.java
File metadata and controls
59 lines (56 loc) · 1.67 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
49
50
51
52
53
54
55
56
57
58
59
package leetcode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Q140_WordBreakII {
public static List<String> wordBreak(String s, Set<String> wordDict) {
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for(int i=1; i<=s.length(); i++){
for(String word:wordDict){
if ( (i >= word.length())
&& (s.substring(i-word.length(), i).equals(word))
&& (dp[i-word.length()]) ){
dp[i] = true;
continue;
}
}
}
List<List<String>> ans = new ArrayList<List<String>>();
ans.add(new ArrayList<String>());
if (dp[s.length()]) {
ans.get(0).add("");
for (int i = 1; i <= s.length(); i++) { // 依次枚举间隔
ans.add(new ArrayList<String>());
for (int j = 0; j < i; j++) {
String temp = s.substring(j, i);
if (dp[j] && dp[i] && wordDict.contains(temp)) {
for (String str : ans.get(j)) {
ans.get(i).add(str + (str.equals("") ? "" : " ") + temp);
}
}
}
}
return ans.get(s.length());
}else
return ans.get(0);
}
public static void main(String[] args) {
Set<String> wordDict = new HashSet<String>();
wordDict.add("a");
wordDict.add("aa");
wordDict.add("aaa");
wordDict.add("aaaa");
wordDict.add("aaaaa");
wordDict.add("aaaaaa");
wordDict.add("aaaaaaa");
wordDict.add("aaaaaaaa");
wordDict.add("aaaaaaaaa");
wordDict.add("aaaaaaaaaa");
List<String> ans = wordBreak("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab",wordDict);
for(String s:ans){
System.out.println(s);
}
}
}