← 返回 airbnb 的题目列表Text Justification with Punctuation Constraint and Multi-Article Separation
类型:online_judge
Problem: Multi-Article Text Formatting (Text Justification)
Given a list of article strings articles and an integer maxWidth, format each article into multiple lines under the following rules:
Each line’s length must not exceed maxWidth.
Words must never be split across lines (line breaks can only occur between words).
No line may start with a punctuation character (e.g., , . ! ? : ; ) ] }, etc.).
If a normal break would cause the next line to begin with punctuation, adjust the break position accordingly.
When possible, try to avoid producing a line that contains only one word (you may adjust boundaries between adjacent lines).
Insert a separator line exactly equal to:
----
between consecutive articles.
Input (implementation contract)
articles: list of strings, each string is one article with words separated by spaces.
maxWidth: integer.
Output
Return a list of strings lines representing all formatted lines (including ---- lines between articles).
Constraints (reasonable interview assumptions)
1 <= len(articles) <= 50
1 <= maxWidth <= 200
Each article length is within 1..10000 characters
Examples / Tests
(Any output satisfying the constraints is acceptable.)
Case 1
Input:
articles = ["Hello , world !"]
maxWidth = 7
A valid output could be:
Hello ,
world !
Case 2
Input:
articles = ["A quick brown fox jumps over the lazy dog"]
maxWidth = 10
One valid output:
A quick
brown fox
jumps over
the lazy
dog
Case 3
Input:
articles = ["one two three", "four five"]
maxWidth = 8
Output:
one two
three
----
four
five
Case 4
Input:
articles = ["Hi !"]
maxWidth = 3
Output:
Hi !
Case 5
Input:
articles = ["wait ... what ?"]
maxWidth = 8
One valid output:
wait ...
what ?
Example
Input
1
10
Hello , world !
Output
Hello ,
world !