Format a Centered Newspaper Page
Problem statement
You are given an array of paragraphs paragraphs and an integer width. Each paragraph is an ordered array of words.
Format the paragraphs in order. Within one paragraph, preserve word order and separate adjacent words with one space. Greedily place as many consecutive words as possible on each line without making the text longer than width. Paragraphs do not share a line.
Center every packed line inside exactly width character positions. If a line needs s spaces, place floor(s / 2) before the text and the remaining spaces after it. Thus, when s is odd, the extra space goes on the right.
Draw a newspaper border with * characters. The first and last returned strings consist of width + 2 asterisks. Wrap every centered content line with one leading and one trailing asterisk.
Return the complete bordered page as a string array.
Function
formatCenteredNewspaperPage(paragraphs: String[][], width: int) → String[]Examples
Example 1
paragraphs = [["The","quick","brown","fox"],["jumps","over","it"]]width = 10return = ["************","*The quick *","*brown fox *","*jumps over*","* it *","************"]The first paragraph packs into The quick and brown fox; each needs one extra space on the right. The second packs jumps over exactly and centers it with four spaces on each side. The top and bottom borders each contain 12 asterisks.
Example 2
paragraphs = [["a","bb","ccc"]]width = 4return = ["******","*a bb*","*ccc *","******"]a bb exactly fills the content width. The remaining word ccc needs one padding space, which goes on the right before the closing border.
Constraints
1 <= paragraphs.length <= 104- Every paragraph contains at least one non-empty word.
1 <= word.length <= width <= 104- The total number of words and characters is at most
105. - Words contain visible non-space ASCII characters.