Problem · String

Print Sentences as Table

EasyAirbnbFULLTIMEPHONE SCREEN

Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem.

You are given n sentences and a fixed table content width width. Print each sentence as one row in a bordered table.

Input

The first line contains two integers n and width. The next n lines each contain one sentence. Sentences may contain spaces, and each sentence length is guaranteed to be at most width.

Output

For each of the n sentences, output rows in the following pattern: before each sentence row output a border line, and after the last sentence row output one final border line, for a total of n+1 border lines interleaved with n sentence rows (if n > 0).

  • A border line is +, followed by exactly width + 2 dash (-) characters, followed by +.
  • A sentence row is |, followed by one space, followed by the sentence, followed by enough trailing spaces so that the sentence plus all trailing spaces together equal exactly width characters, followed by one space, followed by |. The total number of characters between the two | characters is thus width + 2.

If n = 0, return no output lines.

Function Description

Complete solvePrintSentencesAsTable. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters.

Examples
01 · Example 1
input = "3 55\nHello world\nHow are you today\nBye"
return = ["+---------------------------------------------------------+", "| Hello world                                             |", "+---------------------------------------------------------+", "| How are you today                                       |", "+---------------------------------------------------------+", "| Bye                                                     |", "+---------------------------------------------------------+"]
The border line uses width+2=57 dashes. Each sentence row is '|' + ' ' + sentence + trailing_spaces + ' ' + '|', where (sentence length) + (trailing spaces count) = width = 55, and the one leading space and one trailing space account for the remaining 2 characters, giving width+2=57 characters between the pipes. For example, 'Hello world' has length 11, so it is followed by 44 trailing spaces, then a space, for a total of 55+2=57 characters between the pipes.
Constraints

  • 0 <= n <= 10^5
  • 1 <= width <= 10^4
  • Each sentence contains printable ASCII characters and spaces.
  • Each sentence length is at most width.

More Airbnb problems
drafts saved locally
public String[] solvePrintSentencesAsTable(String input) {
    // write your code here
}
input"3 55\nHello world\nHow are you today\nBye"
expected["+---------------------------------------------------------+", "| Hello world |", "+---------------------------------------------------------+", "| How are you today |", "+---------------------------------------------------------+", "| Bye |", "+---------------------------------------------------------+"]
checking account