FastPrepRender Wrapped Two-Party Chat Lines

Render Wrapped Two-Party Chat Lines

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA
Learn

Problem statement

Each message is [sender, text], where sender is 1 or 2. Split text into consecutive character chunks of at most contentWidth; preserve every character, including spaces. An empty text produces one empty chunk.

Pad every chunk with spaces to exactly totalWidth: sender 1 is left-aligned and sender 2 is right-aligned. Return all rendered lines in message order.

Function

renderChat(messages: String[][], contentWidth: int, totalWidth: int) → String[]

Examples

Example 1

messages = [["1","hello"],["2","world"]]contentWidth = 5totalWidth = 8return = ["hello   ","   world"]

The same-width texts align to opposite sides.

Example 2

messages = [["1","abcdef"]]contentWidth = 3totalWidth = 5return = ["abc  ","def  "]

Text is divided into consecutive chunks.

Constraints

  • 1 <= contentWidth <= totalWidth <= 1000
  • The total message text length is at most 100000.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public String[] renderChat(String[][] messages, int contentWidth, int totalWidth) {
    // Write your code here.
}
messages[["1","hello"],["2","world"]]
contentWidth5
totalWidth8
expected["hello ", " world"]
Checking account…