Loading...
You are given a list of strings words and an integer width. No word contains a space, and no word is longer than width.
Lay the words out, in order, on lines of exactly width characters each:
Return the lines as a list of strings.
Input: words = ["This", "is", "an", "example", "of", "text", "justification."], width = 16
Output: ["This is an", "example of text", "justification. "]
Explanation: "This", "is", and "an" have 8 letters and "example" would push the line past 16, so the 8 spare spaces are split across the 2 gaps as 4 and 4. "example", "of", and "text" have 13 letters, so 3 spaces go across 2 gaps as 2 and 1, the left gap getting the extra one. The last line holds one word and is padded with 2 spaces on the right.
Input: words = ["What", "must", "be", "acknowledgment", "shall", "be"], width = 16
Output: ["What must be", "acknowledgment ", "shall be "]
Explanation: The first line has 10 letters and 6 spaces split evenly as 3 and 3. "acknowledgment" is alone on its line because "shall" doesn't fit next to it, so it is padded on the right rather than spread. The last line is "shall be" followed by 8 spaces, not "shall be", because the last line is never spread out.
Input: words = ["Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"], width = 20
Output: ["Science is what we", "understand well", "enough to explain to", "a computer. Art is", "everything else we", "do "]
Explanation: The first line has 15 letters and 5 spaces across 3 gaps, so the gaps are 2, 2, and 1. "understand" and "well" leave 6 spaces for a single gap. "enough to explain to" fills its 20 characters with single spaces. The last line is "do" followed by 18 spaces.
words.length ≤300words[i].length ≤20words[i] consists of English letters and symbols (never a space)width ≤100words[i].length ≤ width