-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordBreak2.java
More file actions
86 lines (70 loc) · 3.14 KB
/
Copy pathWordBreak2.java
File metadata and controls
86 lines (70 loc) · 3.14 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
// 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.
// See: https://leetcode.com/problems/word-break-ii/
// See: https://leetcode.com/explore/featured/card/july-leetcoding-challenge/548/week-5-july-29th-july-31st/3406/
package leetcode.dynamic_programming;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class WordBreak2 {
/**
* Backtracking solution + memorization.
* TODO: Try to find cleaner solution.
*/
List<String> res = new ArrayList<>();
public List<String> wordBreak(String s, List<String> wordDict) {
if (!canBreak(s, wordDict))
return res;
backtrack(s, "", "", 0, new HashSet<>(wordDict));
return res;
}
private void backtrack(String s, String currWord, String currRes, int pos, Set<String> dict) {
if (pos == s.length()) {
if (currWord.isEmpty())
res.add(currRes.trim());
return;
}
String newWord = currWord + s.charAt(pos);
if (dict.contains(newWord)) {
res.add(newWord);
backtrack(s, "", currRes + " " + newWord, pos + 1, dict);
res.remove(newWord);
}
backtrack(s, newWord, currRes, pos + 1, dict);
}
/**
* Checks whether the string is breakable.
*/
public boolean canBreak(String s, List<String> wordDict) {
Set<String> wDict = new HashSet<>(wordDict);
int[] memo = new int[s.length() + 1];
return recur(s.toCharArray(), 0, "", wDict, memo);
}
private boolean recur(char[] sentence, int start, String curr, Set<String> wDict, int[] memo) {
if (start == sentence.length)
return wDict.contains(curr);
boolean res = false;
if (wDict.contains(curr)) {
if (memo[start + 1] == 0) {
res = recur(sentence, start + 1, "" + sentence[start], wDict, memo);
memo[start + 1] = res == true ? 1 : -1;
} else
res = memo[start + 1] == 1 ? true : false;
}
return res || recur(sentence, start + 1, curr + sentence[start], wDict, memo);
}
public static void main(String[] args) {
System.out.println(new WordBreak2().wordBreak("catsanddog",
List.of("cat", "cats", "and", "sand", "dog")));
System.out.println(new WordBreak2().wordBreak("pineapplepenapple",
List.of("apple", "pen", "applepen", "pine", "pineapple")));
System.out.println(new WordBreak2().wordBreak("catsandog",
List.of("cats", "dog", "sand", "and", "cat")));
System.out.println(new WordBreak2().wordBreak("".repeat(75) + "b" + "".repeat(75),
List.of("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa",
"aaaaaaaaa", "aaaaaaaaaa")));
}
}