LeetCode – Text Justification (Java)

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. "

Java Solution

There is not a special algorithm required to solve this problem. To correctly solve this problem, the following situations should be taken care of:
1. if a line has only one word and the word's length is less than max width, we need to fill the left part with spaces.
2. how to distribute extra spaces for each words when the number of spaces can not be evenly distributed to each word.

public List<String> fullJustify(String[] words, int maxWidth) {
    List<String> result = new ArrayList<String>();
    if(words==null || words.length==0){
        return result;
    int count=0;
    int last=0;
    ArrayList<String> list = new ArrayList<String>();
    for(int i=0; i<words.length; i++){
        count = count + words[i].length();
            int wordsLen = count-words[i].length();
            int spaceLen = maxWidth-wordsLen;
            int eachLen = 1;
            int extraLen = 0;
                eachLen = spaceLen/(i-last-1);
                extraLen = spaceLen%(i-last-1);
            StringBuilder sb = new StringBuilder();
            for(int k=last; k<i-1; k++){
                int ce = 0;
                    sb.append(" ");
                    sb.append(" ");
            sb.append(words[i-1]);//last words in the line
            //if only one word in this line, need to fill left with space
                sb.append(" ");
            last = i;
    int lastLen = 0;
    StringBuilder sb = new StringBuilder();
    for(int i=last; i<words.length-1; i++){
        count = count+words[i].length();
        sb.append(words[i]+" ");
    int d=0;
        sb.append(" ");
    return result;
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

Leave a comment