Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth 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 maxWidth characters.

Return the earliest hour at which the shop must be closed to incur a minimum penalty.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does 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.

Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
Output:
          [
            "This    is    an",
            "example  of text",
            "justification.  "
          ]
Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
Output:
        [
          "What   must   be",
          "acknowledgment  ",
          "shall be        "
        ]
Explanation: Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified. Note that the second line is also left-justified because it contains only one word.
Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is", "everything","else","we","do"], maxWidth = 20
Output:
        [
          "Science  is  what we",
          "understand      well",
          "enough to explain to",
          "a  computer.  Art is",
          "everything  else  we",
          "do                  "
        ]
Constraints:

Contents

In this approach, we will loop through the input array of words, and when the maxLength is reached, that is after applying minimum number of spaces between words, for example if there are three words ["cs", "code", "io"], that means we have to include two spaces between 3 words "cs code io" and there are no leading spaces, and trailing spaces except if it is a single word, or the last line (both must be left justified), then add the spaces between the words as per the rules and add it to result.

Implementation steps:
import java.util.ArrayList; import java.util.List; public class TextJustification { static List<String> fullJustify(String[] words, int maxWidth) { List<String> result = new ArrayList<>(); List<StringBuilder> currentLine = new ArrayList<>(); int currentLength = 0; for(String word : words) { if(currentLength + (currentLine.size()) + word.length() > maxWidth) { int totalSpaces = maxWidth - currentLength; // total spaces available int spacesDistro = totalSpaces / Math.max(1, currentLine.size() -1); // Math.max is to avoid divide by zero int remainingSpaces = totalSpaces % Math.max(1, currentLine.size() -1); // now add spaces at the end of strings for(int i=0; i<Math.max(1, currentLine.size()-1); i++) { // we don't need spaces for last word, but if there is only one word we need to StringBuilder sb = currentLine.get(i); for(int j=0; j<spacesDistro; j++) { // add required number of spaces sb.append(" "); } // + add remainingSpaces in greedy fashion if(remainingSpaces > 0) { sb.append( " "); remainingSpaces --; } } String justifiedLine = String.join("",currentLine); result.add(justifiedLine); //reset below, once a line is justified currentLine.clear(); currentLength = 0; } currentLine.add(new StringBuilder(word)); currentLength+=word.length(); } // add the last line here, since it may not have entered into if condition above if(!currentLine.isEmpty()) { String lastLine = String.join(" ", currentLine); int remainingSpaces = maxWidth - lastLine.length(); result.add(lastLine + (" ".repeat(remainingSpaces))); } return result; } public static void main(String[] args) { System.out.println(fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16)); System.out.println(fullJustify(new String[]{"What","must","be","acknowledgment","shall","be"}, 16)); System.out.println(fullJustify(new String[]{"Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"}, 20)); } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the length of words array.
Space complexity: O(n) for the result list.

Above implementations source code can be found at GitHub link for Java code