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:
L:
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.
Corner Cases:
The solution to this question is quite straightforward. But you must write it down very carefully!
- A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.
Java: TextJustification
public class Solution {
public ArrayList<String> fullJustify(String[] words, int L) {
ArrayList<String> result=new ArrayList<String>();
int i=0;
while(i<words.length)
{
//store all words of a line together
ArrayList<String> line=new ArrayList<String>();
int len=0;
while(len+words[i].length()<=L)
{
line.add(words[i]);
len+=words[i].length()+1;
i++;
if(i==words.length)
break;
}
StringBuffer sb=new StringBuffer();
//case1: only one word in a line
if(line.size()==1)
{
String word1=line.get(0);
sb.append(word1);
for(int j=0;j<L-word1.length();j++)
{
sb.append(" ");
}
result.add(sb.toString());
}
//case2: last line
else if(i==words.length)
{
int totalLength=0;
for(int j=0;j<line.size()-1;j++)
{
String str=line.get(j);
sb.append(str);
sb.append(" ");
totalLength+=str.length()+1;
}
String lastWord=line.get(line.size()-1);
sb.append(lastWord);
totalLength+=lastWord.length();
for(int j=0;j<L-totalLength;j++)
{
sb.append(" ");
}
result.add(sb.toString());
}
//case 3: justify from two ends
else
{
int totalLength=line.get(0).length();
int lineSize=line.size();
for(int j=1;j<lineSize;j++)
{
totalLength+=line.get(j).length();
}
int numSpace=L-totalLength;
int avgSpace=numSpace/(lineSize-1);
int remainSpace=numSpace%(lineSize-1);
for(int j=0;j<lineSize-1;j++)
{
sb.append(line.get(j));
//distribute spaces evenly
for(int k=0;k<avgSpace;k++)
{
sb.append(" ");
}
//distribute remaining spaces
if(remainSpace>0)
{
sb.append(" ");
remainSpace--;
}
}
String lastWord=line.get(lineSize-1);
sb.append(lastWord);
result.add(sb.toString());
}
}
return result;
}
}
public ArrayList<String> fullJustify(String[] words, int L) {
ArrayList<String> result=new ArrayList<String>();
int i=0;
while(i<words.length)
{
//store all words of a line together
ArrayList<String> line=new ArrayList<String>();
int len=0;
while(len+words[i].length()<=L)
{
line.add(words[i]);
len+=words[i].length()+1;
i++;
if(i==words.length)
break;
}
StringBuffer sb=new StringBuffer();
//case1: only one word in a line
if(line.size()==1)
{
String word1=line.get(0);
sb.append(word1);
for(int j=0;j<L-word1.length();j++)
{
sb.append(" ");
}
result.add(sb.toString());
}
//case2: last line
else if(i==words.length)
{
int totalLength=0;
for(int j=0;j<line.size()-1;j++)
{
String str=line.get(j);
sb.append(str);
sb.append(" ");
totalLength+=str.length()+1;
}
String lastWord=line.get(line.size()-1);
sb.append(lastWord);
totalLength+=lastWord.length();
for(int j=0;j<L-totalLength;j++)
{
sb.append(" ");
}
result.add(sb.toString());
}
//case 3: justify from two ends
else
{
int totalLength=line.get(0).length();
int lineSize=line.size();
for(int j=1;j<lineSize;j++)
{
totalLength+=line.get(j).length();
}
int numSpace=L-totalLength;
int avgSpace=numSpace/(lineSize-1);
int remainSpace=numSpace%(lineSize-1);
for(int j=0;j<lineSize-1;j++)
{
sb.append(line.get(j));
//distribute spaces evenly
for(int k=0;k<avgSpace;k++)
{
sb.append(" ");
}
//distribute remaining spaces
if(remainSpace>0)
{
sb.append(" ");
remainSpace--;
}
}
String lastWord=line.get(lineSize-1);
sb.append(lastWord);
result.add(sb.toString());
}
}
return result;
}
}