29. ZigZag Conversion

Medium · String

Write a string in a zigzag pattern across a given number of rows, then read it back line by line.

You're given a string `s` and a number `numRows`. Imagine writing the string in a zigzag pattern: start at the top row, move down one row per character until you reach the bottom row, then move back up one row per character until you reach the top row again, and repeat. Once the entire string is written in this zigzag pattern, read all characters row by row from top to bottom to produce your answer.

For example, with "PAYPALISHIRING" and 3 rows, the zigzag looks like: P . . . A . . . H . . . I . . . . A . I . S . I . R . I . N . G . . . Y . . . L . . . . . . . . .

Reading top to bottom gives: "PAHNAPLSIIGYIR".

Examples

Example 1
Input: ["PAYPALISHIRING", 3]
Output: "PAHNAPLSIIGYIR"
Explanation: Three-row zigzag concatenated row by row
Example 2
Input: ["PAYPALISHIRING", 4]
Output: "PINALSIGYAHRPI"
Explanation: Four-row zigzag concatenated row by row

Constraints