Description
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:
Input:
words = [“This”, “is”, “an”, “example”, “of”, “text”, “justification.”]
maxWidth = 16
Output:
[
“This is an”,
“example of text”,
"justification. "
]
Example 2:
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:
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
给一个word[],和一行最多的字符数maxWidth,单词之间凑不满如果不够的话就用space填上,最后一行则是正常的空格,最后用space填充。
For each row, count the length of each word, and intitial a last pointer, which is the the index of the first word in next row. While count not exceed the max width, count we could add how many words in this row.
After get last, new a Stringbuilder and append first word. Then we calculate how many space we will have which is diff = last - 1 - index. If it is the last row or only one word in this row, append words and append rest spaces.
If it is not last row, count the basic length of space which is (maxWidth - count) / diff. We may have remain = (maxWidth - count) % diff, add them to left spaces first. We start add space first, then add one remain and a normal space then words. After that, add this sb into res, and remember move pointers forward.
Code
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
int index = 0;
while (index < words.length){
//count: count this length of this line's words
int count = words[index].length();
//last: index of last word in this line
int last = index + 1;
while (last < words.length){
//out of bound
if (words[last].length() + count + 1 > maxWidth){
break;
}
count += 1 + words[last].length();
last++;
}
StringBuilder sb = new StringBuilder();
//append the frist word of this row
sb.append(words[index]);
//get the number of spaces of this row
int diff = last - 1 - index;
//if it is the last row, add word with normal space and fill rest space
//if this line could only contain one word diff == 0.
if(last == words.length || diff == 0){
for (int i = index + 1; i < last; i++){
sb.append(" ");
sb.append(words[i]);
}
for (int i = sb.length(); i < maxWidth; i++){
sb.append(" ");
}
}
else{//not the last row, we should assign left more than right
//count every space length
int spaces = (maxWidth - count) / diff;
int remain = (maxWidth - count) % diff;
for (int i = index + 1; i < last; i++){
for (int j = spaces; j > 0; j--){
sb.append(" ");
}
if (remain > 0){
sb.append(" ");
remain--;
}
sb.append(" ");
sb.append(words[i]);
}
}
res.add(sb.toString());
index = last;
}
return res;
}
}
Time Complexity: O()
Space Complexity: O()