Render Chat Conversation
Problem statement
Render a conversation between two users. Each row of messages is [user, text], where user is "1" or "2".
Each message starts on a new line. Wrap its words greedily into lines of at most userWidth characters: append the next word with one separating space when it fits, otherwise start the next line. Never split a word. Every word is short enough to fit.
- Lines from user
"1"are left-aligned withinwidthcharacters, padded with spaces on the right. - Lines from user
"2"are right-aligned withinwidthcharacters, padded with spaces on the left. - Surround every content line with
|on both sides. - The first and last lines consist of
+, followed bywidthasterisks, followed by+.
Return the rendered lines in order. Text is case-sensitive, with words separated by exactly one space and no leading or trailing spaces.
Function
renderChatConversation(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 |","+***************+"]The first message wraps into Hello, how r, and u. The second wraps into good and ty. The last wraps into me, too, and bro. User 1 is left-aligned and user 2 is right-aligned in the fifteen-character interior.
Example 2
messages = [["2","ab c"],["1","D"]]width = 4userWidth = 4return = ["+****+","|ab c|","|D |","+****+"]ab c exactly fills the available width. The separate message D begins its own line and receives three spaces of right padding.
Constraints
1 <= messages.length <= 100- Every row contains exactly two strings: a user identifier and nonempty text.
1 <= userWidth <= width <= 100- Each text contains at most
1000printable ASCII characters and has single spaces between words. - Every word has length at most
userWidth.