Render a Messenger Window
Problem statement
Two users are chatting in an app. Render their conversation in a messenger window.
Each row of messages is [user, text], where user is "1" or "2" and names the sender. The rows are in the order the messages were sent.
- Every message starts on a new line.
userWidthis the most characters one line of a message can hold. A longer message continues on more lines.- Never split a word. Fill each line greedily: add the next word, with one space before it, while the line stays within
userWidthcharacters. Otherwise, start a new line with that word. A new line never starts with a space. - The window is
widthcharacters wide. Lines from user"1"are aligned to the left and lines from user"2"to the right. Pad with spaces so every line is exactlywidthcharacters. - Put a
|at both ends of every message line. - The first and last lines of the window are a
+, thenwidthasterisks (*), then another+.
Return all lines of the window from top to bottom.
Function
renderMessengerWindow(messages: String[][], width: int, userWidth: int) → String[]Examples
Example 1
messages = [["1","Hello how r u"],["2","good ty"],["2","u"],["1","me too bro"]]width = 15userWidth = 5return = ["+***************+","|Hello |","|how r |","|u |","| good|","| ty|","| u|","|me |","|too |","|bro |","+***************+"]With userWidth = 5, the first message becomes Hello, how r, and u. Adding u to how r would need 7 characters, so u moves to a new line, and the space before it is dropped. The two messages from user "2" sit against the right border. me too bro takes three lines because no two of its words fit together in 5 characters.
Example 2
messages = [["2","ab cd e"],["1","xyz"]]width = 6userWidth = 5return = ["+******+","| ab cd|","| e|","|xyz |","+******+"]ab cd uses all 5 characters allowed by userWidth, so e starts a new line. The window is 6 characters wide, so lines from user "2" get spaces on the left: one before ab cd and five before e. The line xyz from user "1" gets three spaces on the right.
Constraints
1 <= messages.length <= 100- Each
messages[i]holds exactly two strings: the user, which is"1"or"2", and the text. 1 <= messages[i][1].length <= 1001 <= userWidth <= width <= 100- Each text is words separated by single spaces, with no space at the start or end. Words use English letters, digits, and the marks
.,,,!, and?. - No word is longer than
userWidth.