Format a Newspaper Page
Learn this problemProblem statement
You are given an array of paragraphs paragraphs, an array aligns, and an integer width.
paragraphs[i]is theith paragraph, represented as an ordered array of words.aligns[i]is eitherLEFTorRIGHTand specifies how paragraphiis aligned.widthis the exact number of characters in every formatted output line.
Format the paragraphs in order. Within each paragraph, preserve the word order and separate adjacent words with one space. Greedily place as many consecutive words as possible on each line without making its length exceed width. If the next word does not fit, start a new line with that word.
After packing a line, fill its remaining positions with spaces. For LEFT alignment, append the spaces after the words. For RIGHT alignment, prepend the spaces before the words.
Return all formatted lines in paragraph order as a string array. Each returned string must contain exactly width characters.
Function
formatNewspaperPage(paragraphs: String[][], aligns: String[], width: int) → String[]Examples
Example 1
paragraphs = [["The","quick","brown","fox"],["jumps","over","it"]]aligns = ["LEFT","RIGHT"]width = 10return = ["The quick ","brown fox ","jumps over"," it"]The first paragraph is left-aligned. The quick and brown fox each use 9 characters, so each receives one trailing space. The second paragraph is right-aligned. jumps over already uses all 10 characters, while it receives eight leading spaces.
Constraints
paragraphs.length = aligns.length.- Every value in
alignsis eitherLEFTorRIGHT. widthis positive.- Every word has length at most
width.
More Capital One problems
- Compare Counts Around PivotOA · Seen Jul 2026
- Laser Robot Safe PathOA · Seen Jul 2026
- Match Consecutive Word BoundariesOA · Seen Jul 2026
- Reconstruct Landmark JourneyOA · Seen Jul 2026
- Rightmost Longest Character RunOA · Seen Jul 2026
- Track Received Byte RangesOA · Seen Jul 2026
- Alternate String EndsOA · Seen Jul 2026
- Round-Robin WDL OrderOA · Seen Jul 2026