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. 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. Note: - A word is defined as a character sequence consisting of non-space characters only. - Each word's length is guaranteed to be greater than 0 and not exceed maxWidth. - The input array words contains at least one word.
Example 1
Input: words = ["This","is","an","example","of","text","justification."], maxWidth = 16
Output: ["This is an","example of text","justification. "]
Explanation: Words are packed greedily: "This is an" fits in 16 with 8 characters of text, so the 8 spaces are split evenly, 4 and 4. "example of text" has 13 letters, so its 3 spaces go 2 and 1 (left gap first). The last line is left-justified and padded on the right.
Example 2
Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
Output: ["What must be","acknowledgment ","shall be "]
Explanation: "What must be" has 10 letters, so each of its two gaps gets 3 spaces. "acknowledgment" is alone on its line, so it is left-justified with 2 trailing spaces. The last line, "shall be", is left-justified and padded to 16.
Example 3
Input: words = ["a"], maxWidth = 5
Output: ["a "]
Explanation: The single word is the last line, so it is left-justified and padded to 5 characters.
Greedily fit as many words as possible on each line, then distribute the leftover spaces as evenly as possible between the gaps (left gaps get the extra when it doesn't divide evenly). The last line — and any single-word line — is left-justified.
Run checks all cases above. Submit evaluates all test cases.