Thursday, January 8, 2015

LeetCode 68: 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 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.
public class Solution {
    public List<String> fullJustify(String[] words, int L) {
        List<String> result = new LinkedList<>();
        int n = words.length;
        
        if (n == 0)
            return result;
        
        // Special case: Only one null word ""
        if (words[0].equals(""))
        {
            String line = "";
            
            for (int i = 0; i < L; i++)
                line += " ";
            
            result.add(line);
            
            return result;
        }
        
        // Used to store the words of a line
        Queue<String> queue = new LinkedList<>();    
        int len = 0;
        int wordLen = 0; // Length of words of line (not incluing spaces)
        int spaceLen; // Length of spaces of line
        
        int cnt = 0;
        
        while (cnt < n)
        {
            if (len+words[cnt].length() <= L)
            {
                queue.offer(words[cnt]);
                len += words[cnt].length();
                wordLen += words[cnt].length();
                len++; // At least one space between words
                
                // Last line
                if (cnt == n-1)
                {
                    String line = "";
                    spaceLen = L-wordLen; // Length of spaces of line
                    
                    // In last line, every word only has one space followed.
                    while (!queue.isEmpty())
                    {
                        line += queue.poll();
                        line += " ";
                        spaceLen--;
                    }
                    
                    // Remove the space following the last word.
                    line = line.substring(0, line.length()-1);
                    spaceLen++;
                    
                    // Fill spaces to L
                    while (spaceLen > 0)
                    {
                        line += " ";
                        spaceLen--;
                    }
                    
                    result.add(line);
                }
                
                cnt++;
            }
            else
            {
                int m = queue.size(); // Number of words of line
                spaceLen = L-wordLen; // Length of spaces of line

                int[] spaceNum = new int[queue.size()]; // Number of spaces following word
                
                if (queue.size() == 1) // Only one word a line
                    spaceNum[0] = spaceLen;
                else
                {
                    // Distribute spaces after words
                    for (int i = 0; i < m-1; i++)
                        if (i < spaceLen%(m-1))
                            spaceNum[i] = spaceLen/(m-1)+1;
                        else
                            spaceNum[i] = spaceLen/(m-1);
                            
                    spaceNum[m-1] = 0; // No spaces follows last word if more than one word a line
                }
                
                String line = "";
                
                for (int i = 0; i < m; i++)
                {
                    line += queue.poll();
                    
                    for (int j = 0; j < spaceNum[i]; j++)
                        line += " ";
                }
                
                result.add(line);
                
                len = 0;
                wordLen = 0;
            }  
        }
        
        return result;
    }
}

No comments:

Post a Comment