![]() |
VOOZH | about |
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: PAHNAPLSIIGYIR.
Therefore, for given string str and an integer N, the task is to print the string formed by concatenating N rows when str is written in row-wise Zig-Zag fashion.
Example:
Input: str = "PAYPALISHIRING", N = 3
Output: PAHNAPLSIIGYIRInput: str = "ABCDEFGH", N = 2
Output: ACEGBDFH
Explanation: The input string can be written in Zig-Zag fashion in 2 rows as follows:
A C E G
B D F H
Hence, upon reading the above pattern row-wise, the output string is "ACEGBDFH"
Approach: The given problem is an implementation based problem that can be solved by following the below steps
Below is the implementation of the above approach:
PAHNAPLSIIGYIR
Time Complexity: O(N)
Auxiliary Space: O(N)