This question evaluates string manipulation, greedy packing, and precise space-distribution skills, assessing competency in algorithmic implementation and careful handling of formatting edge cases.
You are given an array of strings words (each string is a word with no spaces) and an integer maxWidth.
Return a list of lines (strings) that formats the words into lines of exactly maxWidth characters each.
maxWidth
).
k
words.
k-1
mandatory gaps
between
adjacent words; each such gap must have
at least 1 space
.
k+1 gaps
:
k-1
gaps
between words
,
words: string[]
,
maxWidth: int
string[]
where each string has length exactly
maxWidth
.
If words = ["a","b","c"] and maxWidth = 5:
"a b c"
has length 5, so output is
["a b c"]
.
1 <= words.length <= 300
1 <= len(words[i]) <= maxWidth <= 100
words[i]
contains no spaces.