Fixed-Width Text Formatter
AirbnbOnsiteMedium
You are laying text out for a display with a fixed character width. Write format_text, which returns the layout as a list of strings.
Part 1: Greedy lines
Given a string text of words separated by single spaces and an integer width, split the words into lines of exactly width characters, and return the lines.
Keep the words in their original order and fill each line greedily: put as many words on a line as fit, with one space between neighbours, and start the next line with the word that did not fit. Words are never split. Pad every line on the right with spaces to exactly width characters, including the last.
- Example 1
- text
"this is a article"- width
10- Output
["this is a ","article "]
Why:
this is atakes 9 characters, and addingarticlewould make 17, so the line is closed and padded with one space.articleis 7 characters and is padded with 3 spaces.
Constraints
1 <= len(text) <= 10^51 <= width <= 100texthas no leading or trailing space and no two adjacent spaces- Every word in
textis at mostwidthcharacters long 'a' <= text[i] <= 'z'ortext[i] == ' '