Format a Newspaper Page
Learn this problemProblem statement
You are moderating a newspaper page, and you have to align the text on the page properly. The text is provided to you in the following format:
paragraphsis an array of paragraphs, where each paragraph is represented as an array of words;alignsis an array representing the alignment of each paragraph fromparagraphs- each element is either"LEFT"or"RIGHT";widthrepresents the maximum number of characters each line of the output can include.
Your task is to produce a newspaper page according to the following specifications:
- For each paragraph
paragraphs[i], include all the wordsparagraphs[i][j]in order, separated by spaces; - Include as many words as possible per each page line (the length of the line must be less than or equal to
width), and put the next word on a new line if it would exceed the limit; - In the case of excess whitespace, words from
paragraphs[i]should be aligned according toaligns[i]- ifaligns[i] = "LEFT", the line should have trailing spaces, ifaligns[i] = RIGHT, it should have leading spaces; - Include a border of
*characters around all the edges of the result - these characters don't count toward thewidth, they are just added to make output more pretty.
It is guaranteed that it is possible to justify the given paragraphs to the newspaper. Return the resulting newspaper page as an array of strings.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(paragraph.length · paragraph[0].length · width) will fit within the execution time limit.
Function
formatNewspaperPage(paragraphs: String[][], aligns: String[], width: int) → String[]Examples
Example 1
paragraphs = [["hello","world"],["How","areYou","doing"],["Please look","and align","to right"]]aligns = ["LEFT","RIGHT","RIGHT"]width = 16return = ["******************","*hello world *","*How areYou doing*","* Please look*","* and align*","* to right*","******************"]For paragraphs = [["hello", "world"], ["How", "areYou", "doing"], ["Please look", "and align", "to right"]], aligns = ["LEFT", "RIGHT", "RIGHT"], and width = 16, the output should be
solution(paragraphs, aligns, width) = ["******************",
"*hello world *",
"*How areYou doing*",
"* Please look*",
"* and align*",
"* to right*",
"******************"]Let's consider all paragraphs:
paragraphs[0] = ["hello", "world"], both words fit on one line with a combined length of11. Sincewidth = 16, there are16 - 11 = 5excess spaces we'll need to add to this line. Becausealigns[0] = "LEFT", we align the text to the left by adding the spaces onto the end of the line. So the result for this paragraph is the line"hello world ".paragraphs[1] = ["How", "areYou", "doing"], all three words fit on one line with a length of16. There are no excess spaces, so the result is"How areYou doing".paragraphs[2] = ["Please look", "and align", "to right"], the words"Please look"and"and align"are too long to combine (the result would have a length of21 > 16), so we'll start a new line with"and align"; furthermore, the words"and align"and"to right"would have a combined length of18 > 16, which is also too long, so"to right"will also be on a new line. Sincealigns[2] = "RIGHT", all excess spaces are added to the beginning of each line. Thus, the result of this paragraph is" Please look" " and align" " to right"
To create the border, we'll add an asterisk (* character) to the beginning and end of each line, and we'll also add a string of width + 2 asterisks before the first paragraph and after the last paragraph.
Constraints
- Input/Output
[execution time limit] 0.5 seconds (cpp)[memory limit] 1 GB[input] array.array.string paragraphsAn array of arrays of strings representing text for the newspaper page.