forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordBreakII.java
69 lines (61 loc) · 2.13 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
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.*;
/**
* 这题是个典型的DFS,不过为了加速用了缓存避免重复计算
* https://leetcode.com/articles/word-break-ii/
*/
public class WordBreakII {
/**
* 下面这种写法可以AC,不过当wordDict很大时则效率堪忧
*/
public List<String> wordBreak(String s, Set<String> wordDict) {
return dfs(s, wordDict, new HashMap<String, List<String>>());
}
private List<String> dfs(String s, Set<String> wordDict, Map<String, List<String>> map) {
if (s.length() == 0) {
return Arrays.asList("");
}
List<String> result = map.get(s);
if (result != null) {
return result;
} else {
result = new LinkedList<String>();
map.put(s, result);
}
for (String word : wordDict) {
if (s.startsWith(word)) {
List<String> list = dfs(s.substring(word.length()), wordDict, map);
for (String text : list) {
result.add(word + (text.length() > 0 ? " " + text : ""));
}
}
}
return result;
}
/**
* 这种写法耗时16ms,效率不错
*/
public List<String> wordBreak(String s, List<String> wordDict) {
HashMap<String, List<String>> cache = new HashMap<>();
cache.put("", Arrays.asList(""));
return dfs(s, new HashSet<String>(wordDict), cache);
}
private List<String> dfs(String s, HashSet<String> wordDict, HashMap<String, List<String>> cache) {
if (cache.containsKey(s)) {
return cache.get(s);
}
List<String> result = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
String t = s.substring(i);
if (wordDict.contains(t)) {
List<String> list = dfs(s.substring(0, i), wordDict, cache);
if (list != null) {
for (String ss : list) {
result.add((ss.length() > 0 ? ss + " " : "") + t);
}
}
}
}
cache.put(s, result);
return result;
}
}