-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextJustification.java
113 lines (96 loc) · 3.22 KB
/
TextJustification.java
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
package org.sean.greedy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/***
* 68. Text Justification
*/
public class TextJustification {
public List<String> fullJustify(String[] words, int maxWidth) {
int cnt = words.length;
int left = 0;
int right = 0;
List<String> output = new ArrayList<>();
while (right < cnt) {
int sz = words[left].length();
if (sz == maxWidth) { // already fit the whole line?
output.add(words[left]);
right = left = left + 1;
continue;
}
right = left + 1;
while (right < cnt) {
sz += words[right].length();
if (sz + 1 <= maxWidth) {// one blank
sz++;
right++;
} else {
break;
}
}
StringBuilder builder = genStringBuilder(words, maxWidth, right, left);
output.add(builder.toString());
if (right < cnt) {
left = right;
}
}
refineLastStrIfNeeded(maxWidth, output);
return output;
}
private static void refineLastStrIfNeeded(int maxWidth, List<String> output) {
int outCnt = output.size();
String last = output.get(outCnt - 1);
String[] splits = last.split("\\s+");
System.out.println(Arrays.toString(splits));
if (splits.length > 1) {
StringBuilder builder = new StringBuilder();
int lineCharCnt = 0;
for (int i = 0; i < splits.length; i++) {
builder.append(splits[i]);
lineCharCnt += splits[i].length();
if (i != splits.length - 1) {
builder.append(div);
lineCharCnt++;
}
}
if (lineCharCnt < maxWidth) {
int dlt = maxWidth - lineCharCnt;
while (dlt > 0) {
builder.append(div);
dlt--;
}
}
output.set(outCnt - 1, builder.toString());
}
}
private static final char div = ' ';
private static StringBuilder genStringBuilder(String[] words, int maxWidth, int right, int left) {
// add [left, right-1]
StringBuilder builder = new StringBuilder();
int subWordCnt = right - left;
int blankCnt = subWordCnt - 1;
if (blankCnt == 0) {
blankCnt = 1;
}
int charSum = 0;
for (int i = left; i < right; i++) {
charSum += words[i].length();
}
int blanks = maxWidth - charSum;
int perBlank = blanks / blankCnt;
int dltBlank = blanks % blankCnt;
for (int i = left; i < right; i++) {
builder.append(words[i]);
if (i != right - 1 || left == right - 1) { // not for the last one
for (int j = 0; j < perBlank; j++) {
builder.append(div);
}
if (dltBlank > 0) {
builder.append(div);
dltBlank--;
}
}
}
return builder;
}
}