Showing:
Handle Score
@Ibrahim Nash 6420
@mb1973 5578
@Quandray 5231
@akhayrutdinov 5111
@saiujwal13083 4510
@sanjay05 3762
@kirtidee18 3673
@marius_valentin_dragoi 3522
@sushant_a 3459
@verma_ji 3412
Concatenation of Zig-Zag String in n Rows
Medium Accuracy: 68.73% Submissions: 243 Points: 4

Given a string and number of rows ‘n’. Print the string formed by concatenating n rows when the input string is written in row-wise Zig-Zag fashion.

Example 1:

Input:
str = "ABCDEFGH"
n = 2
Output: "ACEGBDFH"
Explanation:
Let us write input string in
Zig-Zag fashion in 2 rows.
A   C   E   G
B   D   F   H
Now concatenate the two rows and ignore
spaces in every row. We get "ACEGBDFH"

Example 2:

Input:
str = "GEEKSFORGEEKS"
n = 3
Output: GSGSEKFREKEOE
Explanation:
Let us write input string in
Zig-Zag fashion in 3 rows.
G       S       G       S
E   K   F   R   E   K
E       O       E
Now concatenate the two rows and ignore spaces
in every row. We get "GSGSEKFREKEOE"

You need not read input or print anything. Your task is to complete the function convert() which takes 2 arguments(string str, integer n) and returns the resultant string.

Expected Time Complexity: O(|str|).
Expected Auxiliary Space: O(|str|).

Constraints:
1 ≤ N ≤ 105