-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay61.java
More file actions
50 lines (44 loc) · 1.67 KB
/
Day61.java
File metadata and controls
50 lines (44 loc) · 1.67 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
import java.util.*;
public class Day61 {
public static List<String> wordBreak(String S, Set<String> dictionary) {
List<String> result = new ArrayList<>();
wordBreakHelper(S, dictionary, 0, new ArrayList<>(), result);
return result;
}
public static void wordBreakHelper(String S, Set<String> dictionary, int start, List<String> currentSentence, List<String> result) {
int n = S.length();
if (start == n) {
result.add(String.join(" ", currentSentence));
return;
}
for (int end = start + 1; end <= n; end++) {
String word = S.substring(start, end);
if (dictionary.contains(word)) {
currentSentence.add(word);
wordBreakHelper(S, dictionary, end, currentSentence, result);
currentSentence.remove(currentSentence.size() - 1);
}
}
}
public static void main(String[] args) {
int T = 1;
List<String> testCases = Arrays.asList(
"godisnowherenowhere"
);
List<Set<String>> dictionaries = Arrays.asList(
new HashSet<>(Arrays.asList("god", "is", "no", "now", "where", "here"))
);
for (int i = 0; i < T; i++) {
String S = testCases.get(i);
Set<String> dictionary = dictionaries.get(i);
List<String> sentences = wordBreak(S, dictionary);
if (sentences.isEmpty()) {
System.out.println("No output to be printed");
} else {
for (String sentence : sentences) {
System.out.println(sentence);
}
}
}
}
}