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

This is a hard question which requires extremely careful about handling string.

I use a Word model class to track word and spaces needed after it.

Three cases:

  1. Only one word on this line, append whitespaces after it.
  2. More than one word on this line, keep iterating the list of words and increase whitespaces until meet expected line length.
  3. For last line only one whitespace is inserted between words and additional whitespace will be inserted in the end to meet the expected line length.
public static class Word {
        String word;
        int spaces;

        public Word(String word, int spaces) {
            this.word = word;
            this.spaces = spaces;
        }

        public int getSpaces() {
            return this.spaces;
        }

        public String getWord() {
            return this.word;
        }

        public void addSpace() {
            this.spaces++;
        }
    }

    private String insertLastLine(List<Word> words, int L) {
        StringBuilder sb = new StringBuilder();
        int currentLength = 0;
        for (int i=0; i<words.size(); i++) {
            String word = words.get(i).getWord();
            sb.append(word);
            currentLength = currentLength + word.length();
            if (i==words.size()-1) {
                while (currentLength<L) {
                    sb.append(" ");
                    currentLength = currentLength + 1;
                }
            } else {
                sb.append(" ");
                currentLength = currentLength + 1;
            }        
        }
        return sb.toString();
    }

    private String insertWhitespace(List<Word> words, int currentLength, int maxLength) {
        StringBuilder sb = new StringBuilder();
        int spaces = maxLength-currentLength;
        if (words.size()==1) {
            for (int i=0; i<spaces; i++) {
                words.get(0).addSpace();
            }
        } else {
            while (spaces>0) {
                for (int j=0; j<words.size()-1; j++) {
                    if (spaces>0) {
                        words.get(j).addSpace();
                        spaces--;
                    }
                }         
            }
        }

        for (Word word : words) {
            sb.append(word.getWord());
            for (int j=0; j<word.getSpaces(); j++) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    public List<String> fullJustify1(String[] words, int L) {
        List<String> results = new ArrayList<String>();

        if (words==null||words.length==0) {
            return results;
        }

        int currentLength = 0;
        List<Word> list = new ArrayList<Word>();

        for (int i=0; i<words.length; i++) {
            String newWord = words[i];
            int newLength = currentLength+newWord.length();
            //count required whitespaces between words
            int newSpaces = list.size()+1-1;
            //if this word has to be in the next line
            if (newLength+newSpaces>L) {
                //handle current line
                String line = insertWhitespace(list, currentLength, L);
                results.add(line);

                list.clear();
                currentLength = 0;
            } 

            Word word = new Word(newWord, 0);
            list.add(word);
            currentLength = currentLength + newWord.length();

            //handle last line
            if (i==words.length-1) {
                String line = insertLastLine(list, L);
                results.add(line);
            }
        }
        return results;
    }