-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordBreak.java
More file actions
35 lines (28 loc) · 1.05 KB
/
WordBreak.java
File metadata and controls
35 lines (28 loc) · 1.05 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
package dp.commonsubsequence;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class WordBreak {
public static boolean wordBreak(String s, List<String> wordDict) {
List<String> res = new ArrayList<>();
Set<String> set = new HashSet<>(wordDict);
Boolean[] dp = new Boolean[s.length() + 1];
return helper(res, s, new ArrayList<>(), set, 0, dp);
}
public static boolean helper(List<String> res, String s, List<String> list, Set<String> set, int index, Boolean[] dp) {
if (index == s.length()) return true;
if (dp[index] != null) return dp[index];
for (int i = index; i < s.length(); i++) {
String sub = s.substring(index, i + 1);
if (set.contains(sub)) {
list.add(sub);
if (helper(res, s, list, set, i + 1, dp)) {
return dp[index] = true;
}
list.remove(list.size() - 1);
}
}
return dp[index] = false;
}
}