-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordBreakII.java
More file actions
39 lines (33 loc) · 1.15 KB
/
WordBreakII.java
File metadata and controls
39 lines (33 loc) · 1.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
class Solution {
public void dfs(String[][] subStr, Set<String> wordSet, int n, int i, String s, List<String> result) {
if (i >= n) {
result.add(s);
return;
}
for (int j = i; j < n; j++) {
if (wordSet.contains(subStr[i][j])) {
dfs(subStr, wordSet, n, j + 1, s + " " + subStr[i][j], result);
}
}
}
public List<String> wordBreak(String s, List<String> wordDict) {
int n = s.length();
String[][] subStr = new String[n][n];
for (int i = 0; i < n; i++) {
StringBuilder sb = new StringBuilder();
for (int j = i; j < n; j++) {
sb.append(s.charAt(j));
subStr[i][j] = sb.toString();
}
}
List<String> result = new ArrayList();
Set<String> wordSet = wordDict.stream()
.collect(Collectors.toSet());
for (int j = 0; j < n; j++) {
if (wordSet.contains(subStr[0][j])) {
dfs(subStr, wordSet, n, j + 1, subStr[0][j], result);
}
}
return result;
}
}