-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextJustification.java
More file actions
259 lines (212 loc) · 9.42 KB
/
TextJustification.java
File metadata and controls
259 lines (212 loc) · 9.42 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package Algorithms.Strings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 28 June 2025
* @link 68. Text Justification <a href="https://leetcode.com/problems/text-justification/">LeetCode link</a>
* @topics String, Array, Simulation
*/
public class TextJustification {
public static void main(String[] args) {
String[] words = {"This", "is", "an", "example", "of", "text", "justification."};
int maxWidth = 16;
System.out.printf("fullJustifyMyApproach => %s\n", fullJustifyMyApproach(words, maxWidth));
System.out.printf("fullJustify => %s\n", fullJustify(words, maxWidth));
}
/**
i = nextWordI
lineStats = new int[]{words, len}
spaceBetween = (maxWidth-len)/(words-1) ----> word-1 cause we need space between words
maxWidth = 16
["What","must","be","acknowledgment","shall","be"]
1st line= 1234 | 5678 | 90 | 123 --- if we consider last word then len > 16, So skip it
1st line = new int[]{3, 10}
spaceBetween = (maxWidth-len)/(words-1) = (16-10)/2 = 3
1st line = "What must be" ---- done
i = 3
["What","must","be","acknowledgment","shall","be"]
2nd line= 12345678901234 | 56789 --- if we consider last word then len > 16, So skip it
2nd line = new int[]{1, 14}
if (i==n-1 || stats[0]==1) {
// attach space only on right --- not in b/w
}
2nd line = "acknowledgment " ----- done
i = 4
["What","must","be","acknowledgment","shall","be"]
3rd line= 12345 | 67 --- if we consider last word then len > 16, So skip it
3rd line = new int[]{2, 7}
if (i==n-1 || stats[0]==1) {
// attach space only on right --- not in b/w
}
3rd line = "shall be " ----- done
HANDLE:
If the number of spaces on a line does not divide evenly between words
*/
public static List<String> fullJustifyMyApproach(String[] words, int maxWidth) {
int i = 0;
int n = words.length;
List<String> para = new ArrayList<>();
while (i<n) {
// calculate lineStats[] ---
int[] lineStats = new int[2]; // [numOfWords, wordsLen]
for(int start=i; start<n; start++) {
int len = lineStats[1] + words[start].length();
int numOfWords = lineStats[0] + 1;
if((len + numOfWords-1) > maxWidth) { // (len + numOfWords-1) -- EDGE CASE FOR SPACE
break;
}
lineStats[1] = len;
lineStats[0]++;
}
// ADD startI to endI WORDS IN PARAGRAPH ---
int startI = i;
int endI = i + lineStats[0]-1;
int spaceBetween;
int numOfUnevens;
if(endI == n-1 || lineStats[0]==1) {
spaceBetween = 1;
numOfUnevens = 0;
} else {
int totalSpaces = maxWidth - lineStats[1]; // space between words --> combination of both even or uneven spaces between words
spaceBetween = totalSpaces / (lineStats[0] - 1);
numOfUnevens = totalSpaces % (lineStats[0] - 1);
// or
// spaceBetween = (maxWidth-lineStats[1])/(lineStats[0]-1);
// numOfUnevens = maxWidth - (lineStats[1]+ spaceBetween*(lineStats[0]-1));
}
StringBuilder space = new StringBuilder().repeat(" ", spaceBetween);
StringBuilder sb = new StringBuilder();
for(; startI<=endI; startI++) {
sb.append(words[startI]);
if(startI != endI) {
sb.append(space);
if(numOfUnevens-- > 0) {
sb.append(" ");
}
}
}
// TRAILING SPACES ---
if(endI == n-1 || lineStats[0]==1) {
int diff = maxWidth - sb.length();
sb.repeat(" ", diff); // or sb.append(new StringBuilder().repeat(" ", diff)) Java 21; or sb.append(" ".repeat(diff)) Java 11; or String.join(" ", Collections.nCopies(diff)) Java 8;
}
para.add(sb.toString());
i = endI + 1;
}
return para;
}
public List<String> fullJustifyMyApproachOld(String[] words, int maxWidth) {
int i = 0;
int n = words.length;
List<String> para = new ArrayList<>();
while (i<n) {
// calculate lineStats[]
int[] lineStats = new int[2]; // [numOfWords, wordsLen]
for(int start=i; start<n; start++) {
int len = lineStats[1] + words[start].length();
int numOfWords = lineStats[0] + 1;
if((len + numOfWords-1) > maxWidth) { // (len + numOfWords-1) -- EDGE CASE FOR SPACE
break;
}
lineStats[1] = len;
lineStats[0]++;
}
// i to i+lineStats[0]
int startI = i;
int endI = i + lineStats[0]-1;
if(endI == n-1 || lineStats[0]==1) {
String str = IntStream.rangeClosed(startI, endI).mapToObj(idx-> words[idx]).collect(Collectors.joining(" "));
int diff = maxWidth - str.length();
para.add(str + " ".repeat(diff));
} else {
int spaceBetweenCounnt = (maxWidth-lineStats[1])/(lineStats[0]-1);
StringBuilder space = new StringBuilder().repeat(" ", spaceBetweenCounnt); // or String.join("", Collections.nCopies(n, " ")); or IntStream.rang(0, n).mapToObj(i-> " ").collect(Collectors.joining(""))🔥
int numOfUnevens = maxWidth - (lineStats[1 ]+ spaceBetweenCounnt*(lineStats[0]-1));
StringBuilder sb = new StringBuilder();
for(; startI<=endI; startI++) {
sb.append(words[startI]);
if(startI != endI) {
sb.append(space);
if(numOfUnevens-- > 0) {
sb.append(" ");
}
}
}
para.add(sb.toString());
// para.add(IntStream.rangeClosed(startI, endI).mapToObj(idx-> words[idx]).collect(Collectors.joining(" ".repeat(spaceBetween))));
}
i = endI + 1;
}
return para;
}
public static List<String> fullJustify(String[] words, int maxWidth) {
List<String> result = new ArrayList<>();
List<String> line = new ArrayList<>(); // or StringBuilder
int length = 0;
int i = 0;
int n = words.length;
while (i < n) {
// can we add word[i] to line?
if(length + line.size() + words[i].length() > maxWidth) { // line.size() == number of words, length == length of all words in line.
// line complete
int extraSpaces = maxWidth - length; // extra spaces - combination of both even or uneven spaces
int spaceEach = extraSpaces / Math.max(1, (line.size()-1)); // Math.max to avoid divide by zero
int unevenSpaces = extraSpaces % Math.max(1, (line.size()-1)); // Math.max to avoid divide by zero
for (int j = 0; j < Math.max(1, line.size()-1); j++) { // Math.max to avoid zero
line.set(j, line.get(j) + " ".repeat(spaceEach));
if (unevenSpaces-- > 0) {
line.set(j, line.get(j) + " ");
}
}
result.add(String.join("", line));
line = new ArrayList<>();
length = 0;
}
line.add(words[i]);
length += words[i].length();
i++;
}
// Handle the last line ---
String lastLine = String.join(" ", line);
int trailingSpaces = maxWidth - lastLine.length();
result.add(lastLine + " ".repeat(trailingSpaces));
return result;
}
public static List<String> fullJustify3(String[] words, int maxWidth) {
List<String> result = new ArrayList<>();
int i = 0;
while (i < words.length) {
int j = i, len = 0;
while (j < words.length && len + words[j].length() + (j - i) <= maxWidth) {
len += words[j].length();
j++;
}
int gaps = j - i - 1;
int spaces = maxWidth - len;
StringBuilder line = new StringBuilder();
if (j == words.length || gaps == 0) {
for (int k = i; k < j; k++) {
line.append(words[k]);
if (k != j - 1) line.append(" ");
}
while (line.length() < maxWidth) line.append(" ");
} else {
int spaceEach = spaces / gaps, extra = spaces % gaps;
for (int k = i; k < j; k++) {
line.append(words[k]);
if (k != j - 1) {
int toAdd = spaceEach + (extra-- > 0 ? 1 : 0);
line.append(" ".repeat(toAdd));
}
}
}
result.add(line.toString());
i = j;
}
return result;
}
}