Repository navigation
Expand file tree
/
Copy pathBB3.java
More file actions
62 lines (47 loc) · 1.85 KB
/
Copy pathBB3.java
File metadata and controls
62 lines (47 loc) · 1.85 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
import java.util.*;
public class BB3 {
public List<String> breakWords(String s, Set<String> wordDict) {
return breakWordsHelper(s, wordDict, new HashMap<String, List<String>>());
}
private List<String> breakWordsHelper(String s, Set<String> wordDict, Map<String, List<String>> memo) {
if (memo.containsKey(s)) {
return memo.get(s);
}
List<String> result = new ArrayList<>();
for (String word : wordDict) {
if (s.startsWith(word)) {
List<String> subList = breakWordsHelper(s.substring(word.length()), wordDict, memo);
for (String subListWord : subList) {
result.add(subListWord);
}
result.add(word);
}
}
memo.put(s, result);
return result;
}
public static void main(String[] args) {
BB3 wordBreak = new BB3();
String[] words = { "cat", "cats", "and", "sand", "dog", "apple", "pen", "applepen", "pine", "pineapple", "cats",
"dog", "sand", "and", "cat", "wash", "rag" };
Set<String> wordDict = new HashSet<>(Arrays.asList(words));
String toBreak1 = "catsanddog";
String toBreak2 = "pineapplepenapple";
String toBreak3 = "helloworld";
List<String> splitWords1 = wordBreak.breakWords(toBreak1, wordDict);
for (String w : splitWords1) {
System.out.print(w + " ");
}
System.out.println();
List<String> splitWords2 = wordBreak.breakWords(toBreak2, wordDict);
for (String w : splitWords2) {
System.out.print(w + " ");
}
System.out.println();
List<String> splitWords3 = wordBreak.breakWords(toBreak3, wordDict);
for (String w : splitWords3) {
System.out.print(w + " ");
}
return;
}
}