Zigzag Conversion

Write the string s in a zigzag pattern on numRows rows: go straight down the rows, then diagonally back up to the top, and repeat. Then read the pattern row by row, left to right, and return the resulting string.

30:00

Zigzag Conversion
medium
Free written solution
Topics
Companies

Write the string s in a zigzag pattern on numRows rows: go straight down the rows, then diagonally back up to the top, and repeat. Then read the pattern row by row, left to right, and return the resulting string.

Example 1:
Input: {"s":"PAYPALISHIRING","numRows":3}
Output: PAHNAPLSIIGYIR
Constraints:
  • 1≤s.length≤10001 \leq s.\text{length} \leq 1000

  • s consists of English letters (lower-case and upper-case), , and ..

  • 1≤numRows≤10001 \leq \text{numRows} \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"PAYPALISHIRING","numRows":3}

numRows = 3 is between 2 and len(s) - 1 = 13, so the characters really zigzag.

s (14 chars)

P

0

A

1

Y

2

P

3

A

4

L

5

I

6

S

7

H

8

I

9

R

10

I

11

N

12

G

13

zigzag on 3 rows

r0
r1
r2

rows

rows[0]

""

rows[1]

""

rows[2]

""

answer = rows joined top to bottom

(empty until the rows are read)

Variables
VariableValue
numRows3
len14
DepthFunction Call
Stack empty
0/25