Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Zigzag Conversion

Medium
stringsimulation

Write the string in a zigzag pattern over numRows rows (down the first column, diagonally up to the top, repeat), then read it row by row.

For "PAYPALISHIRING" with 3 rows:

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

Reading row-by-row gives "PAHNAPLSIIGYIR".

Input

  • Line 1: the string s
  • Line 2: the integer numRows

Output: the converted string.

Example 1
Input:
PAYPALISHIRING
3
Output:
PAHNAPLSIIGYIR
Example 2
Input:
PAYPALISHIRING
4
Output:
PINALSIGYAHRPI
Example 3
Input:
AB
1
Output:
AB

Constraints

  • 1 ≤ s.length ≤ 1000
  • 1 ≤ numRows ≤ 1000
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.