forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordBreakII.java
57 lines (52 loc) · 1.71 KB
/
WordBreakII.java
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
package hard;
import java.util.*;
/**
* Created by fishercoder1534 on 10/4/16.
*/
public class WordBreakII {
public List<String> wordBreak(String s, Set<String> wordDict) {
return dfs(s, wordDict, new HashMap<String, ArrayList<String>>());
}
private List<String> dfs(String s, Set<String> wordDict,
HashMap<String, ArrayList<String>> map) {
if(map.containsKey(s)){
return map.get(s);
}
ArrayList<String> res = new ArrayList<String>();
if(s.length() == 0){
res.add("");
return res;
}
for(String word : wordDict){
if(s.startsWith(word)){
List<String> subList = dfs(s.substring(word.length()), wordDict, map);
for(String sub : subList){
res.add(word + (sub.length() == 0 ? "" : " ") + sub);
}
}
}
map.put(s, res);
return res;
}
public static void main(String...strings){
List<String> temp = new ArrayList<String>();
System.out.println(temp);
List<String> temp2 = new ArrayList<String>(temp);
temp2.add("");
System.out.println(temp2);
WordBreakII test = new WordBreakII();
Set<String> wordDict = new HashSet<>();
wordDict.add("cat");
wordDict.add("cats");
wordDict.add("sand");
wordDict.add("and");
wordDict.add("dog");
String s = "catsanddog";
// List<String> list = test.wordBreak(s, wordDict);
List<String> list = test.wordBreak(s, wordDict);
for(String word : list){
System.out.print(word + ", ");
}
System.out.println();
}
}