forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
64 lines (49 loc) · 1.55 KB
/
Solution.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Oleg Cherednik
* @since 01.02.2019
*/
public class Solution {
public static void main(String... args) {
justify(Arrays.asList("the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"), 16).forEach(System.out::println);
}
public static List<String> justify(List<String> words, int k) {
List<String> lines = new ArrayList<>();
List<String> line = new ArrayList<>();
int length = 0;
for (String word : words) {
if (length + line.size() + line.size() > k) {
lines.add(justify(line, length, k));
line.clear();
length = 0;
}
line.add(word);
length += word.length();
}
if (!line.isEmpty())
lines.add(justify(line, length, k));
return lines;
}
private static String justify(List<String> words, int length, int k) {
int[] spaces = new int[words.size() - 1];
while (length < k) {
for (int i = 0; i < spaces.length && length < k; i++) {
spaces[i]++;
length++;
}
}
int i = 0;
StringBuilder buf = new StringBuilder();
for (String word : words) {
buf.append(word);
if (i < spaces.length) {
for (int j = 0; j < spaces[i]; j++)
buf.append(' ');
i++;
}
}
return buf.toString();
}
}