leetcode 68. text justification

68. Text Justification

Difficulty: Hard

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

Note:

  • A word is defined as a character sequence consisting of non-space characters only.
  • Each word’s length is guaranteed to be greater than 0 and not exceed maxWidth.
  • The input array words contains at least one word.

Example 1:

1
2
3
4
5
6
7
8
9
Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]

Example 2:

1
2
3
4
5
6
7
8
9
10
11
12
Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is "shall be " instead of "shall be",
because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.

Example 3:

1
2
3
4
5
6
7
8
9
10
11
12
13
Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
"to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]

Solution

Language: 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
class  {

char[] spaceChar;
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> result = new ArrayList<>();
if (words == null || maxWidth <= 0) {
return result;
}
spaceChar = new char[maxWidth];
Arrays.fill(spaceChar, ' ');

int cur = 0;
while (cur < words.length) {
cur = justify(words, cur, maxWidth, result);
}
return result;

}

private int justify(String[] words, int cur, int maxWidth, List<String> result) {
int oriCur = cur;
int wordLength = words[cur++].length();
int lenWithSpace = wordLength;
while (cur < words.length && maxWidth - lenWithSpace - 1 >= words[cur].length()) {
wordLength += words[cur].length();
lenWithSpace += words[cur].length() + 1;
cur++;
}
result.add(buildString(words, oriCur, cur, maxWidth, wordLength));
return cur;
}

private String buildString(String[] words, int ori, int cur, int maxWidth, int wordLength) {
StringBuilder sb = new StringBuilder();
if (cur == words.length) {
for (int i = ori; i < cur; i++) {
sb.append(words[i]);
if (i == cur - 1) {
break;
}
sb.append(words[i]);
sb.append(' ');
}
sb.append(spaceChar, 0, maxWidth - sb.length());
} else if (cur - ori == 1) {
sb.append(words[ori]);
sb.append(spaceChar, 0, maxWidth - sb.length());
} else {
int spaces = maxWidth - wordLength;
int baseSpace = spaces / (cur - ori - 1);
int extraSpaceIndex = spaces % (cur - ori - 1) - 1;
for (int i = ori; i < cur; i++) {
sb.append(words[i]);
if (i == cur - 1) {
break;
}
sb.append(spaceChar, 0, baseSpace);
if (i <= ori + extraSpaceIndex) {
sb.append(' ');
}
}
}
return sb.toString();
}


}