-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_140.java
More file actions
43 lines (39 loc) · 1.32 KB
/
Copy pathP_140.java
File metadata and controls
43 lines (39 loc) · 1.32 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
package leetcode.hard;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public final class P_140 {
public List<String> wordBreak(String s, List<String> wordDict) {
final Set<String> dict = new HashSet<>();
int maxLen = 0;
for (String w : wordDict) {
dict.add(w);
maxLen = Math.max(maxLen, w.length());
}
return dfs(0, maxLen, s, dict, new HashMap<>());
}
private static List<String> dfs(int start, int max, String s, Set<String> dict,
Map<Integer, List<String>> dp) {
if (start == s.length()) {
return Collections.singletonList("");
}
if (dp.containsKey(start)) {
return dp.get(start);
}
final List<String> res = new ArrayList<>();
for (int end = start + 1; end <= start + max && end <= s.length(); end++) {
final String curr = s.substring(start, end);
if (dict.contains(curr)) {
for (String sub : dfs(end, max, s, dict, dp)) {
res.add(curr + (sub.isEmpty() ? "" : " ") + sub);
}
}
}
dp.put(start, res);
return res;
}
}