Tuesday, February 11, 2014

Text Justification

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 Lcharacters.
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."]
L16.
Return the formatted lines as:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]
Note: Each word is guaranteed not to exceed L in length.
click to show corner cases.

Solution:
Trivial implementation question. Need to be care of corner cases.
// case1 : last line --->append addtional spaces at the end.
// case2 : one char in a line --->append addtional spaces at the end.
// case3 : no space at the left & right most 
//    ---> assign a space before a word except the first word in a line
public class Solution {
    public ArrayList fullJustify(String[] words, int L) {
        ArrayList result = new ArrayList();
        
        // justify one line
        ArrayList curLine = new ArrayList();
        int curLen  = 0;
        int i = 0;
        while(i < words.length){
            // the current line has enough space
            if(curLen == 0 || curLen + 1 + words[i].length() <= L){
             
                // check if it is the last word in a line
                if(curLen == 0)
                    curLine.add(words[i]);
                else
                    curLine.add(" " + words[i]);
                            
                curLen += curLine.get(curLine.size()-1).length();
                
                
                // last line
                if(i == words.length -1){
                    String str = convert(curLine);
                    
                    // append spaces at the end
                    int diff = L - curLen;
                    for(int k = diff; k > 0; k--)
                        str += " ";
                    result.add(str);
                }
                
                i++;
            }
            
            // the current line can't contain the current word
            else{
                // additional space
                int diff = L - curLen;
                int l = 0;
                
                for(int k = diff; k > 0; k--){
                    
                    // only one char in a line
                    if(curLine.size() == 1){                
                        curLine.set(0, curLine.get(0) + " ");
                    }
                    else{
                        // not assign space after the last word
                        if(l == curLine.size()-1){
                            l = 0;
                        }
                
                        curLine.set(l, curLine.get(l) + " ");
                            l++;
                        }
                    }
                    result.add(convert(curLine));
                
                    // justify in a new line    
                    curLine = new ArrayList();
                    curLen = 0;
                
                }
            }
        
        return result;
    }
    
    public String convert(ArrayList strs){
        StringBuilder sb = new StringBuilder();
        for(String str : strs)
            sb.append(str);
        return sb.toString();
    }
}

No comments:

Post a Comment