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.
Analysis:
For each line, the first word does not need space, for each of the following words, we need count one space. In this way, we find out the words fit into the current line, then construct the string of the current line.
After finding out all words in the current line, we calculate the number of space between each words. Suppose the total number of space is totalSpace, then we should add (totalSpace/(wordNum-1)) between words. In addition, we have (totalSpace%(wordNum-1)) number of space left, we add them from left to right into each gap. NOTE: a special case is that there is only one word in the current line, we then add all spaces after the word.
For the last line, we just add one space in each gap and then add the left spaces on the end of the line.
Solution:
1 public class Solution { 2 public List<String> fullJustify(String[] words, int L) { 3 List<String> res = new ArrayList<String>(); 4 int index = 0; 5 int totalSpace=-1, aveSpace=-1, leftSpace=-1; 6 7 while (index<words.length){ 8 int curLen = 0; 9 List<String> lineWord = new ArrayList<String>(); 10 lineWord.add(words[index]); 11 curLen = words[index].length(); 12 index++; 13 while (index<words.length && curLen+words[index].length()+1<=L){ 14 lineWord.add(words[index]); 15 curLen += words[index].length()+1; 16 index++; 17 } 18 int wordNum = lineWord.size(); 19 20 //if it is not the last line. 21 if (index<words.length){ 22 if (wordNum>1){ 23 totalSpace = L-curLen+(wordNum-1); 24 aveSpace = totalSpace/(wordNum-1); 25 leftSpace = totalSpace%(wordNum-1); 26 String space = ""; 27 for (int i=0;i<aveSpace;i++) space = space+" "; 28 String lineStr = lineWord.get(0); 29 for (int i=1;i<lineWord.size();i++){ 30 lineStr += space; 31 if (leftSpace>0){ 32 lineStr += " "; 33 leftSpace--; 34 } 35 lineStr += lineWord.get(i); 36 } 37 res.add(lineStr); 38 } else { 39 totalSpace = L-lineWord.get(0).length(); 40 String lineStr = lineWord.get(0); 41 for (int i=0;i<totalSpace;i++) lineStr += " "; 42 res.add(lineStr); 43 } 44 } else { 45 leftSpace = L-curLen; 46 String lineStr = lineWord.get(0); 47 for (int i=1;i<wordNum;i++) lineStr += " "+lineWord.get(i); 48 for (int i=0;i<leftSpace;i++) lineStr += " "; 49 res.add(lineStr); 50 } 51 52 } 53 54 return res; 55 } 56 }