### LeetCode  • ㊗️
• 大家
• offer
• 多多！

## Problem

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                  "
]


## Code

class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
int index = 0;

while (index < words.length) {
int nextIndex = findNextIndex(words, index, maxWidth);
String line = justify(index, nextIndex, words, maxWidth);
index = nextIndex;
}

return res;
}

private String justify(int index, int nextIndex, String[] words, int maxWidth) {
StringBuilder sb = new StringBuilder();
int gap = nextIndex - index - 1;
// 1. only one word; 2. last word; 3. multiple words
if (gap == 0) {
sb.append(words[index]);
sb.append(generateSpaces(maxWidth - sb.length()));
} else if (nextIndex == words.length) {
for (int i = index; i < nextIndex; i++) {
sb.append(words[i]);
// last word
if (i == nextIndex - 1) break;
sb.append(" ");
}
sb.append(generateSpaces(maxWidth - sb.length()));
} else {
int currLen = getCurrLen(words, index, nextIndex);
int remain = maxWidth - currLen;
int spaceEachGap = remain / gap;
int spaceRemain = remain % gap;

for (int i = index; i < nextIndex; i++) {
sb.append(words[i]);
// last word
if (i == nextIndex - 1) break;
sb.append(generateSpaces(spaceEachGap + 1));
if (spaceRemain > 0) {
sb.append(" ");
spaceRemain--;
}
}
}

return sb.toString();
}

private int getCurrLen(String[] words, int currIndex, int nextIndex) {
int currLen = words[currIndex].length();

for (int i = currIndex + 1; i < nextIndex; i++) {
currLen += words[i].length() + 1;
}

return currLen;
}

private String generateSpaces(int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(" ");
}

return sb.toString();
}

private int findNextIndex(String[] words, int currIndex, int maxWidth) {
int currLen = words[currIndex].length();
int nextIndex = currIndex + 1;

while (nextIndex < words.length) {
if (words[nextIndex].length() + currLen + 1 > maxWidth) {
break;
}

currLen += words[nextIndex].length() + 1;
nextIndex++;
}

return nextIndex;
}
}


### What if there are long words

For example, word is “civilization”, and maxWidth is 5 Split the word to “civi-“, “liza-“, “tion” and then append them until the last part

class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
int index = 0;

while (index < words.length) {
int nextIndex = findNextIndex(words, index, maxWidth);
// if word length is larger the maxWidth
if (nextIndex - index - 1 == 0 && words[index].length() > maxWidth) {
String sub = words[index].substring(0, maxWidth - 1) + "-";