Skip to main content

Sentence Boxing

easy
Software engineer

You are given a list of sentences, where each element in sentences is a non-empty string containing printable ASCII characters and spaces.

Return the formatted text as a list of strings, where each sentence appears inside its own ASCII box, using the following formatting rules:

  1. Box Width: Each box's inner width is set to the length of the longest sentence in sentences.
  2. Alignment: Each sentence appears left-aligned in its box. If the sentence is shorter than the box width, pad the right side with spaces so its visible length matches the box width.
  3. Borders:
    • The top and bottom use a plus sign + at both corners and hyphens - for the horizontal edges.
    • The sides use vertical bars |.

The output should list all lines in the exact order to be printed: for each sentence, first the top border, then the sentence line, then the bottom border.

Return all lines, including borders and padding, as a list of strings.

Example 1

Input

sentences = ["first word", "my second sentence", "now it's third"]

Output

["+------------------+","|first word        |","+------------------+","|my second sentence|","+------------------+","|now it's third    |","+------------------+"]

Explanation

The longest sentence is "my second sentence" with a length of 18. 
- Each box must have an internal width of 18.
- The top/bottom borders consist of '+' + 18 '-' characters + '+'.
- "first word" (length 10) gets 8 trailing spaces to reach width 18.
- "now it's third" (length 13) gets 5 trailing spaces.
- Every sentence is individually wrapped.

Example 2

Input

sentences = ["a", "bc"]

Output

["+--+","|a |","+--+","|bc|","+--+"]

Example 3

Input

sentences = ["hi"]

Output

["+--+","|hi|","+--+"]

Constraints

  • 1 <= sentences.length <= 100
  • 1 <= length of each sentence <= 100
  • Each sentence contains only printable ASCII characters and spaces.

Screening

ArrayDesignSimulationString
Language
Code editor loads in the browser.

Output

Input

["first word","my second sentence","now it's third"]

Expected

["+------------------+","|first word |","+------------------+","|my second sentence|","+------------------+","|now it's third |","+------------------+"]

Your output

Run to see your output.