Monday, February 10, 2014

Leetcode: 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.

Solution:

 public class Solution {
    public ArrayList<String> fullJustify(String[] words, int L) {
        int wordStartLine = 0;
        int charactersInThisLine = 0;
        int wordsInThisLine = 0;
        ArrayList<String> solution = new ArrayList<String>();
        StringBuilder line = new StringBuilder();
        for(int i=0; i<words.length; i++)
        {
            if(charactersInThisLine+wordsInThisLine+words[i].length()>L)
            {
                line = new StringBuilder();
                int numSpaces = 0;
                int extraSpaces=0;
                if(wordsInThisLine>1)
                {
                    numSpaces= (L-charactersInThisLine)/(wordsInThisLine-1);
                    extraSpaces = (L-charactersInThisLine)%(wordsInThisLine-1);
                }
                for(int j=wordStartLine; j<i; j++)
                {
                    line.append(words[j]);
                    if(j==i-1)
                        break;
                    for(int n=0; n<numSpaces; n++)
                        line.append(' ');
                    if(j-wordStartLine<extraSpaces)
                        line.append(' ');
                }
                for(int j=line.length(); j<L;j++)
                    line.append(' ');
                solution.add(line.toString());
                wordStartLine = i;
                charactersInThisLine = 0;
                wordsInThisLine = 0;
            }
            charactersInThisLine += words[i].length();
            wordsInThisLine ++;
        }
        
        if(solution.size()==0 || wordStartLine<words.length)
        {
            line = new StringBuilder();
            for(int j=wordStartLine; j<words.length; j++)
            {
                line.append(words[j]);
                if(j==words.length-1)
                    break;
                line.append(' ');
            }
            for(int j=line.length(); j<L;j++)
                line.append(' ');
            solution.add(line.toString());
        }
        
        return solution;
    }
}

No comments :

Post a Comment