Skip to content

Practice Zigzag Conversion

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#6
LeetCode Problem

Zigzag Conversion

Problem Statement

Write the characters of s across numRows in a repeating zigzag: move down through the rows, then diagonally back up, and repeat until all characters have been placed. Read the rows from top to bottom, joining the characters in each row from left to right, and return that string. With one row, the result is s unchanged. For PAYPALISHIRING across three rows, the rows are PAHN, APLSIIG and YIR, producing PAHNAPLSIIGYIR.

Source: Zigzag Conversion on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "PAYPALISHIRING", numRows = 3
Output
"PAHNAPLSIIGYIR"

# Example 2

Input
s = "PAYPALISHIRING", numRows = 4
Output
"PINALSIGYAHRPI"

# Example 3

Input
s = "A", numRows = 1
Output
"A"

Constraints

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

Related LeetCode Problems

Practice more problems similar to Zigzag Conversion.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.