Problem

Submissions

Solution

New

Discuss

1

Difficulty: EASY

Avg. time to solve

15 min

Success Rate

85%

Problem Statement

Suggest Edit

```
N = 12, M = 3 and STR = ‘CODINGNINJAS’
```

```
There are three rows (‘M = 3’) in the zig-zag pattern. Row one contains ‘CNN’, row two contains ‘OIGIJS’, and row three contains ‘DNA’. After concatenating the three rows, we get the string ‘CNNOIGIJSDNA’. So, the answer is ‘CNNOIGIJSDNA’.
```

```
1. The string ‘STR’ consists of capital letters only (i.e., characters from ‘A-Z’).
```

```
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, denoting the size of string ‘STR’ and the number of rows in the zig-zag pattern, respectively.
The second line of each test case contains a string ‘STR’.
```

```
For each test case, return the string formed by concatenating all ‘M’ rows when string ‘STR’ is written in a zig-zag pattern.
```

```
You do not need to print anything; it has already been taken care of. Just implement the function
```

```
1 <= T <= 10^2
1 <= N <= 10^3
1 <= M <= N
‘STR’ contains only ‘A-Z’ characters.
Time Limit: 1 second
```

```
2
7 3
ABCDEFG
5 2
NINJA
```

```
AEBDFCG
NNAIJ
```

```
Test Case 1:
```

```
There are three rows (‘m = 3’) in the zig-zag pattern. Row one contains ‘AE’, row two contains ‘BDF’, and row three contains ‘CG’. After concatenating the three rows, we get the string ‘AEBDFCG’. So, the answer is ‘AEBDFCG’.
Test Case 2:
```

```
There are two rows (‘m = 2’) in the zig-zag pattern. Row one contains ‘NNA’, and row two contains ‘IJ’. After concatenating the two rows, we get the string ‘NNAIJ’. So, the answer is ‘NNAIJ’.
```

```
2
4 2
PQRS
6 6
ZIGZAG
```

```
PRQS
ZIGZAG
```

Console