 All Problems
Zigzag Conversion
medium
strings
amazon
microsoft
bloomberg
adobe

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: "PAHNAPLSIIGYIR".

Write the code that will take a string and make this conversion given a number of rows.

Example 1:

Input:
PAYPALISHIRING
3
Output: PAHNAPLSIIGYIR

Example 2:

Input:
PAYPALISHIRING
4
Output: PINALSIGYAHRPI

Example 3:

Input:
A
1
Output: A

Constraints:

  • 1 ≤ s.length ≤ 1000
  • s consists of English letters, ',', and '.'
  • 1 ≤ numRows ≤ 1000

Input format: First line: the string s. Second line: numRows.

Output format: The zigzag-converted string read row by row.

Run to check your code against the sample cases, or submit to run every case