-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-justification.java
72 lines (54 loc) · 1.63 KB
/
text-justification.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
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
int left = 0;
int n = words.length;
while (left < n) {
int len = words[left].length();
int right = left + 1;
while (right < n && (len + words[right].length() + (right - left)) <= maxWidth) {
len += words[right].length();
right++;
}
int extra = maxWidth - len;
if (right - left == 1 || right == n) {
// left justify
res.add(leftJustify(words, left, right, extra));
} else {
// mid justify
res.add(midJustify(words, left, right, extra));
}
left = right;
}
return res;
}
private String leftJustify(String[] words, int left, int right, int extra) {
int rightSpaces = extra - (right - left - 1);
StringBuilder sb = new StringBuilder(words[left]);
for (int i = left + 1; i < right; i++) {
sb.append(" " + words[i]);
}
for (int i = 0; i < rightSpaces; i++) {
sb.append(" ");
}
return sb.toString();
}
private String midJustify(String[] words, int left, int right, int extra) {
int boundaries = right - left - 1;
int spaces = extra / boundaries;
int extraSpaces = extra % boundaries;
StringBuilder sb = new StringBuilder(words[left]);
for (int i = left + 1; i < right; i++) {
int spacesToApply = spaces;
if (extraSpaces > 0) {
spacesToApply++;
extraSpaces--;
}
for (int j = 0; j < spacesToApply; j++) {
sb.append(" ");
}
sb.append(words[i]);
}
return sb.toString();
}
}