All problems

Zigzag Conversion

Medium · strings

A banner prints the letters of a string s in a zigzag over num_rows rows. The first letter goes in the top row. Then each next letter goes one row down, until the bottom row is reached. After that each next letter goes one row up and one column to the right, until the top row is reached again. Then the pattern repeats.

For s = "ABCDEFGHIJKLMN" and num_rows = 4 the banner looks like this:

A     G     M
B   F H   L N
C E   I K
D     J

Read the banner row by row, from the top row to the bottom row, and each row from left to right. Return the string you get. Here the rows read AGM, BFHLN, CEIK and DJ, so the answer is "AGMBFHLNCEIKDJ".

If num_rows is 1, the banner is a single row and the answer is s itself. If there are more rows than letters, the letters just go straight down.

Examples

Example 1

Input: s = "ABCDEFGHIJKLMN", num_rows = 4

Output: "AGMBFHLNCEIKDJ"

The four rows read AGM, BFHLN, CEIK and DJ.

Example 2

Input: s = "ABCDEFGHIJ", num_rows = 3

Output: "AEIBDFHJCG"

The three rows read AEI, BDFHJ and CG.

Example 3

Input: s = "HELLO", num_rows = 1

Output: "HELLO"

With one row nothing moves.

Constraints