-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBreakMultipleWordsWithNoSpaceIntoSpace.java
More file actions
164 lines (149 loc) · 5.45 KB
/
BreakMultipleWordsWithNoSpaceIntoSpace.java
File metadata and controls
164 lines (149 loc) · 5.45 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.*;
public class BreakMultipleWordsWithNoSpaceIntoSpace {
public String breakWord(char[] str,int low,Set<String> dictionary){
StringBuffer buff = new StringBuffer();
for(int i= low; i < str.length; i++){
buff.append(str[i]);
if(dictionary.contains(buff.toString())){
String result = breakWord(str, i+1, dictionary);
if(result != null){
return buff.toString() + " " + result;
}
}
}
if(dictionary.contains(buff.toString())){
return buff.toString();
}
return null;
}
public String breakWordDP(String word, Set<String> dict){
int T[][] = new int[word.length()][word.length()];
for(int i=0; i < T.length; i++){
for(int j=0; j < T[i].length ; j++){
T[i][j] = -1; //-1 indicates string between i to j cannot be split
}
}
//fill up the matrix in bottom up manner
for(int l = 1; l <= word.length(); l++){
for(int i=0; i < word.length() -l + 1 ; i++){
int j = i + l-1;
String str = word.substring(i,j+1);
//if string between i to j is in dictionary T[i][j] is true
if(dict.contains(str)){
T[i][j] = i;
continue;
}
//find a k between i+1 to j such that T[i][k-1] && T[k][j] are both true
for(int k=i+1; k <= j; k++){
if(T[i][k-1] != -1 && T[k][j] != -1){
T[i][j] = k;
break;
}
}
}
}
if(T[0][word.length()-1] == -1){
return null;
}
//create space separate word from string is possible
StringBuffer buffer = new StringBuffer();
int i = 0; int j = word.length() -1;
while(i < j){
int k = T[i][j];
if(i == k){
buffer.append(word.substring(i, j+1));
break;
}
buffer.append(word.substring(i,k) + " ");
i = k;
}
return buffer.toString();
}
public List<String> wordBreakTopDown(String s, Set<String> wordDict) {
Map<Integer, List<String>> dp = new HashMap<>();
int max = 0;
for (String s1 : wordDict) {
max = Math.max(max, s1.length());
}
return wordBreakUtil(s, wordDict, dp, 0, max);
}
private List<String> wordBreakUtil(String s, Set<String> dict, Map<Integer, List<String>> dp, int start, int max) {
if (start == s.length()) {
return Collections.singletonList("");
}
if (dp.containsKey(start)) {
return dp.get(start);
}
List<String> words = new ArrayList<>();
for (int i = start; i < start + max && i < s.length(); i++) {
String newWord = s.substring(start, i + 1);
if (!dict.contains(newWord)) {
continue;
}
List<String> result = wordBreakUtil(s, dict, dp, i + 1, max);
for (String word : result) {
String extraSpace = word.length() == 0 ? "" : " ";
words.add(newWord + extraSpace + word);
}
}
dp.put(start, words);
return words;
}
public boolean wordBreakTopDownOneSolution(String s, Set<String> wordDict) {
Map<Integer, Boolean> dp = new HashMap<>();
int max = 0;
for (String s1 : wordDict) {
max = Math.max(max, s1.length());
}
return wordBreakTopDownOneSolutionUtil(s, wordDict, 0, max, dp);
}
private boolean wordBreakTopDownOneSolutionUtil(String s, Set<String> dict, int start, int max, Map<Integer, Boolean> dp) {
if (start == s.length()) {
return true;
}
if (dp.containsKey(start)) {
return dp.get(start);
}
for (int i = start; i < start + max && i < s.length(); i++) {
String newWord = s.substring(start, i + 1);
if (!dict.contains(newWord)) {
continue;
}
if (wordBreakTopDownOneSolutionUtil(s, dict, i + 1, max, dp)) {
dp.put(start, true);
return true;
}
}
dp.put(start, false);
return false;
}
public boolean wordBreakBottomUp(String s, List<String> wordList) {
boolean[] T = new boolean[s.length() + 1];
Set<String> set = new HashSet<>();
for (String word : wordList) {
set.add(word);
}
T[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
if(T[j] && set.contains(s.substring(j, i))) {
T[i] = true;
break;
}
}
}
return T[s.length()];
}
public static void main(String args[]){
Set<String> dictionary = new HashSet<String>();
dictionary.add("I");
dictionary.add("like");
dictionary.add("had");
dictionary.add("play");
dictionary.add("to");
String str = "Ihadliketoplay";
BreakMultipleWordsWithNoSpaceIntoSpace bmw = new BreakMultipleWordsWithNoSpaceIntoSpace();
String result1 = bmw.breakWordDP(str, dictionary);
System.out.print(result1);
}
}