


If ‘S’ = “beaninjacoder” and ‘ROW’ = 4
Then the zig-zag pattern is:
b j r
e n a e
a i c d
n o
Therefore, we will print “bjrenaeaicdno”.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a string ‘S’, denoting the input string.
The second line of each test case contains a single integer ‘ROW’, denoting the number of rows in the zig-zag pattern to be created.
For each test case, print the new string after zig-zag conversion.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ ‘T’ ≤ 10
1 ≤ |S| ≤ 10000
1 ≤ ‘ROW’ ≤ 10000
S only contains lower case English alphabets
Time limit: 1 sec
Consider the case where the number of rows is equal to 10, and everything has a 0 based indexing. In that case, the input string's indexes form the given pattern:
Here Step-2 involves passing through the lower part of the zig-zag, and Step-1 involves passing the upper part of the zig-zag.
Using some pattern recognition we can find all the indices that lie in a particular row. Let’s say we need to find all the indices that lie in 5’th row, the first element will itself be the 5’th index and the next element can be calculated using the step-2, if curRow stores the current row number and curIdx stores the current index number then the next element will be after 2 * (|S| - 2 - curRow) + 2 indices. Further, we will apply the step-1 as we will now have to pass the upper part of the zig-zag and the next element will now lie after 2 * (curRow - 1) + 2 indices and to find the next element we will again have to apply the step-2 and the process we keep on repeating itself.
In general, we can find all the elements of each row and for a particular row, we can stop the search if the current index exceeds the length of the input string. But we need to cover the border case of the number of rows equal to one separately here, this is because of the way we have defined our pattern recognition formula above.