Zig-Zag String Problem Statement

Given a string STR of size N and an integer M representing the number of rows in the zig-zag pattern, return the string formed by concatenating all rows when the string STR is written in a row-wise zig-zag pattern.

Example:

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

When arranged in a zig-zag pattern with 3 rows, the rows contain the following letters: Row 1: 'CNN', Row 2: 'OIGIJS', Row 3: 'DNA'. Concatenating these rows gives the result: 'CNNOIGIJSDNA'.

Constraints:

  • 1 <= T <= 10^2
  • 1 <= N <= 10^3
  • 1 <= M <= N
  • STR contains only uppercase letters ('A-Z').
  • Time Limit: 1 second
Be the first one to answer
Add answer anonymously...
SAP 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