-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathWordBreakII140.java
More file actions
166 lines (149 loc) · 5.15 KB
/
WordBreakII140.java
File metadata and controls
166 lines (149 loc) · 5.15 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Given a non-empty string s and a dictionary wordDict containing a list of
* non-empty words, add spaces in s to construct a sentence where each word is
* a valid dictionary word. Return all such possible sentences.
*
* Note:
* The same word in the dictionary may be reused multiple times in the segmentation.
* You may assume the dictionary does not contain duplicate words.
*
* Example 1:
* Input:
* s = "catsanddog"
* wordDict = ["cat", "cats", "and", "sand", "dog"]
* Output:
* [
* "cats and dog",
* "cat sand dog"
* ]
*
* Example 2:
* Input:
* s = "pineapplepenapple"
* wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
* Output:
* [
* "pine apple pen apple",
* "pineapple pen apple",
* "pine applepen apple"
* ]
* Explanation: Note that you are allowed to reuse a dictionary word.
*
* Example 3:
* Input:
* s = "catsandog"
* wordDict = ["cats", "dog", "sand", "and", "cat"]
* Output:
* []
*/
public class WordBreakII140 {
// Memory Limit Exceeded
// public List<String> wordBreak(String s, List<String> wordDict) {
// int N = s.length();
// LinkedList<String>[] dp = new LinkedList[N+1];
// for (int i=0; i<=N; i++) dp[i] = new LinkedList<>();
// Set<String> wordSet = new HashSet<>(wordDict);
// dp[0].add("");
// for (int i=1; i<=N; i++) {
// for (int j=0; j<i; j++) {
// String w = s.substring(j, i);
// if (dp[j].size() > 0 && wordSet.contains(w)) {
// for (String p: dp[j]) {
// dp[i].add(p.length() == 0 ? w : (p + " " + w));
// }
// }
// }
// }
// return dp[N];
// }
// DP
public List<String> wordBreak(String s, List<String> wordDict) {
Set<String> wordSet = new HashSet<>(wordDict);
if (!wordIsBreakable(s, wordSet)) return new ArrayList<>();
return wordBreak(s, wordSet);
}
public List<String> wordBreak(String s, Set<String> wordDict) {
LinkedList<String>[] dp = new LinkedList[s.length() + 1];
LinkedList<String> initial = new LinkedList<>();
initial.add("");
dp[0] = initial;
for (int i = 1; i <= s.length(); i++) {
LinkedList<String> list = new LinkedList<>();
for (int j = 0; j < i; j++) {
if (dp[j].size() > 0 && wordDict.contains(s.substring(j, i))) {
for (String l : dp[j]) {
list.add(l + (l.equals("") ? "" : " ") + s.substring(j, i));
}
}
}
dp[i] = list;
}
return dp[s.length()];
}
public boolean wordIsBreakable(String s, Set<String> wordDictSet) {
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && wordDictSet.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}
// Backtracing
public List<String> wordBreak2(String s, List<String> wordDict) {
if (!wordIsBreakable(s, new HashSet<>(wordDict))) return new ArrayList<>();
int N = s.length();
Set<String>[] dp = new Set[N];
for (int i=0; i<N; i++) dp[i] = new HashSet<String>();
for (int i=0; i<N; i++) {
for (String word: wordDict) {
if (s.startsWith(word, i)) {
dp[i].add(word);
}
}
}
List<String> res = new ArrayList<>();
helper(N, dp, res, new StringBuilder(), 0);
return res;
}
private void helper(int N, Set<String>[] dp, List<String> res, StringBuilder sb, int i) {
if (i == N) {
res.add(sb.substring(0, sb.length()-1).toString());
return;
}
for (String w: dp[i]) {
sb.append(w).append(" ");
helper(N, dp, res, sb, i+w.length());
sb.delete(sb.length()-w.length()-1, sb.length());
}
}
/**
* https://leetcode.com/problems/word-break-ii/discuss/44167/My-concise-JAVA-solution-based-on-memorized-DFS
*/
public List<String> wordBreak3(String s, Set<String> wordDict) {
return DFS(s, wordDict, new HashMap<String, LinkedList<String>>());
}
// DFS function returns an array including all substrings derived from s.
List<String> DFS(String s, Set<String> wordDict, HashMap<String, LinkedList<String>>map) {
if (map.containsKey(s))
return map.get(s);
LinkedList<String>res = new LinkedList<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.isEmpty() ? "" : " ") + sub);
}
}
map.put(s, res);
return res;
}
}