Text Justification

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.

Corner Cases:

  1. A line other than the last line might contain only one word. What should you do in this case?
  2. In this case, that line should be left-justified.
public class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> result = new ArrayList<>();
        if (words == null || words.length == 0) {
            return result;
        }
        
        int count = 0, last = 0;
        
        for (int i = 0; i < words.length; i++) {
            // if it is not possible to put ith word in this line
            if (count + words[i].length() + (i - last) > maxWidth) {
                int spaceCount = 0;
                int extraCount = 0;
                
                if (i - last - 1 > 0) { // decrease the gap count
                    spaceCount = (maxWidth - count) / (i - last - 1);
                    extraCount = (maxWidth - count) % (i - last - 1);
                }
                
                StringBuilder sb = new StringBuilder();
                for (int j = last; j < i; j++) {
                    sb.append(words[j]);
                    
                    if (j < i - 1) { // add space if it is not the last word
                        for (int k = 0; k < spaceCount; k++) {
                            sb.append(" ");
                        }
                        
                        if (extraCount > 0) {
                            sb.append(" ");
                            extraCount--;
                        }
                    }
                }
                
                // if only one word in this line, need to fill left with space
                while (sb.length() < maxWidth) {
                    sb.append(" ");
                }
                
                result.add(sb.toString());
                count = 0;
                last = i;
            }
            
            count +=  words[i].length();
        }
        
        // last line
        StringBuilder sb = new StringBuilder();
        for (int i = last; i < words.length; i++) {
            sb.append(words[i]);
            
            if (sb.length() < maxWidth) {
                sb.append(" ");
            }
        }
        while (sb.length() < maxWidth) {
            sb.append(" ");
        }
        
        result.add(sb.toString());
        return result;
    }
}

Hope this helps,
Michael

DigitalOcean Referral Badge