-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-68-Text-Justification.java
More file actions
208 lines (176 loc) · 7.37 KB
/
LeetCode-68-Text-Justification.java
File metadata and controls
208 lines (176 loc) · 7.37 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
/*
https://discuss.leetcode.com/topic/4189/share-my-concise-c-solution-less-than-20-lines/2
*/
public class Solution {
// public List<String> fullJustify(String[] words, int maxWidth) {
// List<String> result = new LinkedList<String>();
// // each loop is Text Justification of one line
// for(int i = 0, w; i < words.length; i = w){
// // Step1: Justify standard texts
// // len: one line's length, w: index of words in one line
// int len = -1;
// // len + words[w].length() + 1 <= maxWidth, not <, as length can get to maxWidth
// for(w = i; w < words.length && len + words[w].length() + 1 <= maxWidth; w++){
// len += words[w].length() + 1;
// }
// // until here, we get standard texts in one line: word + ' ' + word + ' ' ... ' ' + word, each word with one space in gap
// // Step2: distirbute extra spaces
// // now, we start distribute extra spaces in this line. First let's calculate the # of extrace to distribute.
// // w != i + 1: only one word in one line, w != words.length: last line
// // space = 1 not space = 0, because in last line if there are more than one words, we need then have 1 space gap
// // ["What","must","be","shall","be."] -> ["What must be","shall be. "], not ["What must be","shallbe. "]
// int space = 1, extra = 0;
// if(w != i + 1 && w != words.length){
// // maxWidth - len: # of extra spaces to distribute, w - i: # of words in this line, w - i + 1: # of spaces in this line, +1: add the one space gap in standard texts
// space = (maxWidth - len) / (w - i - 1) + 1;
// extra = (maxWidth - len) % (w - i - 1);
// }
// // Step3: Text Justification
// StringBuilder sb = new StringBuilder();
// sb.append(words[i]);
// for(int j = i + 1; j < w; j++){
// for(int s = space; s > 0; s--) sb.append(' ');
// if(extra-- > 0) sb.append(' ');
// sb.append(words[j]);
// }
// // finally, don't forget the spaces for the last line (for last line, there is one space in word gap, and all left are spaces append to the end )
// int strLen = maxWidth - sb.length();
// while(strLen-- > 0) sb.append(' ');
// result.add(sb.toString());
// }
// return result;
// }
/*
https://blog.csdn.net/tingting256/article/details/49804527
https://shmilyaw-hotmail-com.iteye.com/blog/2302045
*/
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> result = new ArrayList<>();
int len = words.length;
if (len <= 0) return result;
int i = 0;
while (i < len) {
int size = words[i].length();
int j = i + 1;
while (j < len && size + 1 + words[j].length() <= maxWidth) {
size += 1 + words[j].length();
j++;
}
String line = words[i];
if (j == len) {
// Case 1. We reached the last line.
for (int k = i + 1; k < j; k++) {
line += " " + words[k];
}
while(line.length() < maxWidth) {
line += " ";
}
result.add(line);
} else {
// means this is not last line
if (j - i == 1) {
// Case 2. A line with only one word
while (line.length() < maxWidth) {
line += " ";
}
result.add(line);
} else {
// Case 3. Normal case.
int numSlot = j - i - 1;
int actualLen = size - numSlot; // the len of pure words in this line (without space)
int spaceLen = maxWidth - actualLen;
int x = spaceLen / numSlot;
int y = spaceLen % numSlot;
int count = 0;
for (int k = i + 1; k < j; k++) {
int spaceInLine = 0;
while (spaceInLine++ < x) line += " ";
if (count++ < y) line += " ";
line += words[k];
}
result.add(line);
}
}
i = j;
}
return result;
}
// Another implementation (easier to understand)
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> result = new ArrayList<>();
List<String> list = new ArrayList<>();
int len = 0;
for (int i = 0; i < words.length; ) {
int tempLen = len + words[i].length();
if (list.size() > 0) {
tempLen += 1;
}
if (tempLen > maxWidth) {
buildString(result, list, maxWidth);
list = new ArrayList<>();
len = 0;
continue;
}
if (list.size() > 0) {
len += 1;
}
len += words[i].length();
list.add(words[i]);
i++;
}
if (list.size() > 0) {
// Add last line
buildLastString(result, list, maxWidth);
}
return result;
}
private void buildString(List<String> result, List<String> list, int maxWidth) {
int n = list.size() - 1;
int lenWords = 0, lenSpace = 0;
for (String str : list) {
lenWords += str.length();
}
lenSpace = maxWidth - lenWords;
StringBuilder sb = new StringBuilder();
sb.append(list.get(0));
// Edge Case 1: the line only has one word, all the spaces left are spaces append to the end
if (list.size() == 1) {
int lenEachSpace = lenSpace;
for (int j = 0; j < lenEachSpace; j++) {
sb.append(" ");
}
result.add(sb.toString());
return;
}
// Normal case
int lenEachSpace = lenSpace / n;
int nEachSpace = lenSpace % n;
for (int i = 1; i < list.size(); i++) {
// Add space
for (int j = 0; j < lenEachSpace; j++) {
sb.append(" ");
}
if (nEachSpace > 0) {
sb.append(" ");
nEachSpace--;
}
// Add word
sb.append(list.get(i));
}
result.add(sb.toString());
}
private void buildLastString(List<String> result, List<String> list, int maxWidth) {
// Edge Case 2: the last line
StringBuilder sb = new StringBuilder();
sb.append(list.get(0));
for (int i = 1; i < list.size(); i++) {
sb.append(" ");
sb.append(list.get(i));
}
int lenSpace = maxWidth - sb.length();
for (int i = 0; i < lenSpace; i++) {
sb.append(" ");
}
result.add(sb.toString());
}
}