Repository navigation
Expand file tree
/
Copy pathTextJustification.java
More file actions
91 lines (79 loc) · 2.9 KB
/
Copy pathTextJustification.java
File metadata and controls
91 lines (79 loc) · 2.9 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
package com.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by carmentano on 23/05/2018.
*/
public class TextJustification {
public static void main(String[] args) {
String[] d = {"This", "is", "an", "example", "of", "text", "justification."};
List<String> strings = new TextJustification().fullJustify(d, 16);
int a = 1;
}
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> result = new ArrayList<>();
List<String> nextLineList = new ArrayList<>();
int totNextLine = 0;
for(int i =0; i < words.length; i++) {
if(nextLineList.isEmpty()) {
nextLineList.add(words[i]); //I am sure of this because of problem constraints
totNextLine += words[i].length();
} else {
if(totNextLine + 1 + words[i].length() <= maxWidth) { //I need at least one space
nextLineList.add(words[i]);
totNextLine += 1 + words[i].length();
} else {
//time to generate the new row
result.add(centerJustified(nextLineList, maxWidth));
nextLineList.clear();
totNextLine = 0;
i--;
}
}
}
if(!nextLineList.isEmpty()) {
result.add(leftJustified(nextLineList, maxWidth));
}
return result;
}
String centerJustified(List<String> words, int maxWitdth) {
StringBuilder sb = new StringBuilder();
int totSpaces = maxWitdth - words.stream().map(s -> s.length()).reduce((a, b) -> a + b).get();
if(words.size() == 1) {
sb.append(words.get(0));
appendSpaces(sb, totSpaces);
} else {
int numSpaceSlots = words.size() - 1;
int ordinarySpaces = totSpaces / numSpaceSlots;
int extraSpaces = totSpaces % numSpaceSlots;
for(int i = 0; i < words.size(); i ++) {
sb.append(words.get(i));
if(i != words.size() - 1) {
if (i < extraSpaces) {
appendSpaces(sb, ordinarySpaces + 1);
} else {
appendSpaces(sb, ordinarySpaces);
}
}
}
}
return sb.toString();
}
String leftJustified(List<String> words, int maxWitdth) {
//left justf
StringBuilder sb = new StringBuilder();
sb.append(words.get(0));
for(int i = 1; i < words.size(); i++) {
sb.append(' ');
sb.append(words.get(i));
}
appendSpaces(sb, maxWitdth - sb.length());
return sb.toString();
}
void appendSpaces(StringBuilder sb, int nspaces) {
for(int i =0; i < nspaces; i++) {
sb.append(' ');
}
}
}