68 Text Justification

Problem

Given an array of words and a length L, format the text such that each line has exactly L 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 L 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.

For example, words: ["This", "is", "an", "example", "of", "text", "justification."] L: 16.

Return the formatted lines as:

``````[
"This    is    an",
"example  of text",
"justification.  "
]
``````

Note: Each word is guaranteed not to exceed L in length.

Solutions

``````public class Solution {
public List<String> fullJustify(String[] words, int L) {
List<String> res = new ArrayList<String>();
List<String> helper = new ArrayList<String>();
int curL = 0;
for (int i = 0; i < words.length; i ++){
int wordL = words[i].length();
if (curL != 0){
curL +=1;
}
curL += wordL;

if (curL > L){
curL = curL - wordL - 1;
int num = helper.size();
int totalSpaceNum = 0;
int eachSpaceNum = 0;
String eachSpace = "";
if (num !=1) {
totalSpaceNum = L - curL + num - 1;
eachSpaceNum = totalSpaceNum / (num - 1);
for (int k = 0; k < eachSpaceNum; k ++){
eachSpace += " ";
}
}
String plusSpace = eachSpace + " ";
//num - 1's space
int diff = totalSpaceNum - eachSpaceNum *(num-1);

for (int j = 0; j < helper.size(); j ++){
if (j == 0){
}
else if (j <= diff){
}
else{
}
}
helper.clear();
curL = 0;
i --;
}
else if (curL == L){
for (int j = 0; j < helper.size(); j ++){
if (j == 0){
}
else{
}
}
helper.clear();
curL = 0;
}
else{
//curL < L
}
}//for i

if (!helper.isEmpty()){
for (int k = 0; k < helper.size(); k ++){
if (k == 0){
}
else{
}
}
}
return res;
}
int append = L - add.length();
for (int k = 0; k < append; k ++){
}
}
}
``````
``````class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
int count = 0;
for (int i = 0; i < words.length; i ++) {
String s = words[i];
int newCount = 0;
if (count == 0) {
newCount = s.length();
}
else {
newCount = count + 1 + s.length();
}
if (newCount <= maxWidth) {
count = newCount;
}
else {
String newLine = getLine(line, count, maxWidth);
line.clear();
count = s.length();
}
}

String newLine = getLastLine(line, maxWidth);

return res;
}
private String getLastLine(List<String> line, int width) {
StringBuilder sb = new StringBuilder();
for (String word:line) {
sb.append(word + " ");
}
sb.deleteCharAt(sb.length() - 1);
while (sb.length() < width) {
sb.append(" ");
}
return sb.toString();
}
private String getLine(List<String> line, int count, int width) {
int numOfSpace = line.size() - 1;
int addSpaceCount = width - count;
String equalSpace = "";
int bonusCount = 0;
if (numOfSpace != 0) {
int equalSpaceCount = addSpaceCount / numOfSpace;
for (int i = 0; i < equalSpaceCount; i ++) {
equalSpace += " ";
}
}
StringBuilder sb = new StringBuilder();
int index = 0;
for (String word:line) {
sb.append(word);
if (index < bonusCount) {
sb.append(" " + equalSpace + " ");
}
else if (index != line.size() - 1) {
sb.append(" " + equalSpace);
}
index ++;
}
while (sb.length() < width) {
sb.append(" ");
}
return sb.toString();
}
}
``````