-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextJustification.java
60 lines (52 loc) · 2.29 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
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> justifiedText = new ArrayList<>();
int index = 0;
int numOfWords = words.length;
while (index < numOfWords) {
List<String> currentLineWords = new ArrayList<>();
int currentLineWidth = 0;
// Try to fit as many words in the current line as possible
while (index < numOfWords && currentLineWidth + words[index].length() + currentLineWords.size() <= maxWidth) {
currentLineWords.add(words[index]);
currentLineWidth += words[index].length();
index++;
}
// Justify the current line
StringBuilder justifiedLine = new StringBuilder();
int totalSpaces = maxWidth - currentLineWidth;
int numWords = currentLineWords.size();
// If it's the last line or there's only one word, left-justify
if (index >= numOfWords || numWords == 1) {
for (int i = 0; i < numWords; i++) {
justifiedLine.append(currentLineWords.get(i));
if (i < numWords - 1) {
justifiedLine.append(" ");
}
}
while (justifiedLine.length() < maxWidth) {
justifiedLine.append(" ");
}
} else {
// Distribute spaces evenly between words
int spaceBetweenWords = totalSpaces / (numWords - 1);
int extraSpaces = totalSpaces % (numWords - 1);
for (int i = 0; i < numWords; i++) {
justifiedLine.append(currentLineWords.get(i));
if (i < numWords - 1) {
for (int j = 0; j < spaceBetweenWords; j++) {
justifiedLine.append(" ");
}
// Distribute extra spaces from the left
if (extraSpaces > 0) {
justifiedLine.append(" ");
extraSpaces--;
}
}
}
}
justifiedText.add(justifiedLine.toString());
}
return justifiedText;
}
}