Mid/SeniorString

Zigzag Conversion

Given a string s and an integer numRows, write the characters of s in a zigzag pattern on numRows rows.

The zigzag pattern is formed by moving:

  • Downward row by row from the top row to the bottom row.
  • Then diagonally upward row by row back to the top row.
  • Repeating this process until every character in s has been placed.

Return the string produced by reading the rows of the zigzag pattern from top to bottom.

Example 1
Inputs = "PAYPALISHIRING", numRows = 3
Output"PAHNAPLSIIGYIR"
Writing the string in 3 rows and reading row by row gives PAHNAPLSIIGYIR.
Example 2
Inputs = "PAYPALISHIRING", numRows = 4
Output"PINALSIGYAHRPI"
Writing the string in 4 rows and reading row by row gives PINALSIGYAHRPI.

Constraints

  • 1 <= s.length <= 1000
  • s consists of English letters (lower-case and upper-case), ',' and '.'.
  • 1 <= numRows <= 1000

Asked at 19 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate