We’re preparing your current view and syncing the latest data.
Given a string s and an integer numRows, arrange the characters of s in a zigzag pattern on numRows rows. Then read the characters row by row to form a new string. The pattern is formed by writing characters diagonally and vertically in a zigzag shape.
For example, the string "PAYPALISHIRING" with numRows = 3 will be written as: P A H N A P L S I I G Y I R
Reading line by line gives "PAHNAPLSIIGYIR" as output.
Implement a function to perform this conversion.
Return a string which is formed by concatenating characters line by line from the zigzag pattern.
Example 1
Input
s = "PAYPALISHIRING", numRows = 3
Output
"PAHNAPLSIIGYIR"
Explanation
The zigzag pattern is: P A H N A P L S I I G Y I R Concatenating rows gives the output.
Example 2
Input
s = "PAYPALISHIRING", numRows = 4
Output
"PINALSIGYAHRPI"
Explanation
The zigzag pattern is: P I N A L S I G Y A H R P I Concatenating rows gives the output.