Zig-Zag String Problem Statement

Given a string 'STR' of length 'N' and an integer 'M' representing the number of rows in the zig-zag pattern, your task is to return the string formed by concatenating all 'M' rows when 'STR' is arranged in a zig-zag pattern.

Input:

The first line contains an integer 'T' representing the number of test cases.
Each test case begins with two integers 'N' and 'M', representing the length of the string 'STR' and the number of rows, respectively.
The next line contains the string 'STR'.

Output:

For each test case, return the concatenated string from the zig-zag pattern of 'STR' over 'M' rows.

Example:

Input:
N = 12, M = 3, STR = 'CODINGNINJAS'
Output:
'CNNOIGIJSDNA'
Explanation:

In a zig-zag pattern with 3 rows, row 1 is 'CNN', row 2 is 'OIGIJS', and row 3 is 'DNA'. The concatenation is 'CNNOIGIJSDNA'.

Constraints:

  • 1 ≤ T ≤ 102
  • 1 ≤ N ≤ 103
  • 1 ≤ M ≤ N
  • The string 'STR' contains only uppercase letters ('A-Z').
AnswerBot
1d

Given a string and number of rows, return the concatenated string formed by arranging the input string in a zig-zag pattern over the specified number of rows.

  • Iterate through the string and distribute ...read more

Help your peers!
Add answer anonymously...
Oyo Rooms Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter