-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path140_WrodBreakII.java
63 lines (54 loc) · 2.07 KB
/
140_WrodBreakII.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
// Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
// Return all such possible sentences.
// For example, given
// s = "catsanddog",
// dict = ["cat", "cats", "and", "sand", "dog"].
// A solution is ["cats and dog", "cat sand dog"].
//simpler than mine but still get TLE
public class Solution {
public List<String> wordBreak(String s, Set<String> dict) {
Map<Integer, List<String>> validMap = new HashMap<Integer, List<String>>();
// initialize the valid values
List<String> l = new ArrayList<String>();
l.add("");
validMap.put(s.length(), l);
// generate solutions from the end
for(int i = s.length() - 1; i >= 0; i--) {
List<String> values = new ArrayList<String>();
for(int j = i + 1; j <= s.length(); j++) {
if (dict.contains(s.substring(i, j))) {
for(String word : validMap.get(j)) {
values.add(s.substring(i, j) + (word.isEmpty() ? "" : " ") + word);
}
}
}
validMap.put(i, values);
}
return validMap.get(0);
}
}
//AC
public class Solution {
private final Map<String, List<String>> cache = new HashMap<>();
public List<String> wordBreak(String s, Set<String> set) {
if (cache.containsKey(s)) return cache.get(s);
List<String> res = new LinkedList<>();
if (set.contains(s)) res.add(s);
for(int i=1; i<s.length(); i++) {
String left = s.substring(0, i), right = s.substring(i);
if(set.contains(left) && conSuffix(set, right)) {
for(String ss : wordBreak(right, set)) {
res.add(left + " " + ss);
}
}
}
cache.put(s, res);
return res;
}
private boolean conSuffix(Set<String> set, String str) {
for(int i=0; i<str.length(); i++) {
if(set.contains(str.substring(i)))return true;
}
return false;
}
}