Medium

Quiz

#6 Zigzag Conversion

APPROACH

When the characters of a string are laid out in a zigzag pattern across numRows rows and then read row by row, a new string results. Implement convert(s, numRows) to perform this transformation.

Example 1:

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

Example 2:

string convert(string s, int numRows);

Example 3:

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

Example 4:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I

Example 5:

Input: s = "A", numRows = 1
Output: "A"
1 of 4
1:00

What is the optimal approach for this problem?