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.

click to show corner cases.

Corner Cases:

A line other than the last line might contain only one word. What should you do in this case? In this case, that line should be left-justified.

Solution

public class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
       List<String> result = new ArrayList<>();
        if (Objects.isNull(words) || words.length == 0 || maxWidth < 0) {
            return result;
        }
        int currentSize = 0;

        List<String> line = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            if (currentSize + line.size() + words[i].length() <= maxWidth) {
                currentSize += words[i].length();
                line.add(words[i]);
            } else {
                // one word in the current line
                StringBuffer sb = null;
                if (line.size() == 1) {
                    sb = new StringBuffer(line.get(0));
                    for (int m = line.get(0).length(); m < maxWidth; m++) sb.append(" ");
                } else {
                    // more than one word
                    int divisor = (maxWidth - currentSize) / (line.size() - 1);
                    int remainder = (maxWidth - currentSize) % (line.size() - 1);
                    sb = new StringBuffer();
                    for (int j = 0; j < line.size(); j++) {
                        sb.append(line.get(j));

                        if (j != line.size() - 1) {
                            for (int m = 1; m <= divisor; m++) {
                                sb.append(" ");
                            }
                        }
                        if (remainder > 0) {
                            sb.append(" ");
                            remainder--;
                        }
                    }
                }

                result.add(sb.toString());
                line = new ArrayList<>();
                line.add(words[i]);
                currentSize = words[i].length();
            }
        }

        if (!line.isEmpty()) {
            StringBuffer sb = null;
            if (line.size() == 1) {
                sb = new StringBuffer(line.get(0));
                for (int m = line.get(0).length(); m < maxWidth; m++) sb.append(" ");
            } else {
                // more than one word
                sb = new StringBuffer();
                for (int j = 0; j < line.size(); j++) {
                    sb.append(line.get(j));
                    sb.append(" ");
                }

                for (int m = sb.length(); m < maxWidth; m++) sb.append(" ");

            }

            result.add(sb.toString());
        }

        return result;
    }
}

results matching ""

    No results matching ""