AAlgoLab
ALG-M012MediumSimulation

Zigzag Message Layout

Write characters into the specified number of rows by moving down one row at a time, then up diagonally, repeating at boundaries. Return the concatenation of rows from top to bottom.

Contract

input text: string
input rows: integer
return string

Constraints

  • • rows >= 1
  • • 0 <= text length <= 1000

Examples

Example 1
text = "PAYPALISHIRING"
rows = 3
→ "PAHNAPLSIIGYIR"
Example 2
text = "PAYPALISHIRING"
rows = 4
→ "PINALSIGYAHRPI"

5 judge tests (2 shown above). Target complexity: O(n).