Concatenation of Zig-Zag String in N Rows
Last Updated :
21 Jun, 2022
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: PAHNAPLSIIGYIR.
Therefore, for given string str and an integer N, the task is to print the string formed by concatenating N rows when str is written in row-wise Zig-Zag fashion.
Example:
Input: str = “PAYPALISHIRING”, N = 3
Output: PAHNAPLSIIGYIR
Input: str = “ABCDEFGH”, N = 2
Output: ACEGBDFH
Explanation: The input string can be written in Zig-Zag fashion in 2 rows as follows:
A C E G
B D F H
Hence, upon reading the above pattern row-wise, the output string is “ACEGBDFH”
Approach: The given problem is an implementation based problem that can be solved by following the below steps
- Create an array of N strings, arr[N].
- Initialize direction as “down” and row as 0. The direction indicates whether the current pointer is moving up or down in rows.
- Traverse the input string, do the following for every character.
- Append the current character to the string representing the current row.
- If row number is N – 1, then change direction to ‘up’
- If row number is 0, then change direction to ‘down’
- If direction is ‘down’, do row++. Else do row–.
- One by one print all strings of arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printZigZagConcat(string str, int n)
{
if (n == 1)
{
cout << str << endl;
}
string res = "" ;
string arr[n] = { "" };
bool down;
int row = 0;
for ( int i = 0; i < str.size(); i++)
{
arr[row].push_back(str[i]);
if (row == n - 1)
{
down = false ;
}
if (row == 0)
{
down = true ;
}
if (!down)
row--;
else
row++;
}
for ( int i = 0; i < n; i++)
{
cout << arr[i];
}
}
int main()
{
string str = "PAYPALISHIRING" ;
int N = 3;
printZigZagConcat(str, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void printZigZagConcat(String str, int n)
{
if (n == 1 ) {
System.out.print(str + "\n" );
}
String res = "" ;
String[] arr = new String[n];
for ( int i = 0 ; i < n; i++)
arr[i] = "" ;
boolean down = false ;
int row = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (row >= 0 )
arr[row] += (str.charAt(i));
if (row == n - 1 ) {
down = false ;
}
if (row == 0 ) {
down = true ;
}
if (!down)
row--;
else
row++;
}
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i]);
}
}
public static void main(String[] args)
{
String str = "PAYPALISHIRING" ;
int N = 3 ;
printZigZagConcat(str, N);
}
}
|
Python3
def printZigZagConcat( str , n):
if n = = 1 :
print ( str )
return
l = len ( str )
arr = ["" for x in range (l)]
row = 0
for i in range (l):
arr[row] + = str [i]
if row = = n - 1 :
down = False
elif row = = 0 :
down = True
if down:
row + = 1
else :
row - = 1
for i in range (n):
print (arr[i], end = "")
str = "PAYPALISHIRING"
N = 3
printZigZagConcat( str , N)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static void printZigZagConcat(String str, int n)
{
if (n == 1) {
Console.WriteLine(str);
}
String[] arr = new String[n];
for ( int i = 0 ; i < n ; i++)
arr[i] = "" ;
bool down = false ;
int row = 0;
for ( int i = 0 ; i < str.Length ; i++) {
if (row >= 0)
arr[row] += (str[i]);
if (row == n - 1) {
down = false ;
}
if (row == 0) {
down = true ;
}
if (!down)
row--;
else
row++;
}
for ( int i = 0; i < n; i++) {
Console.Write(arr[i]);
}
}
public static void Main( string [] args){
String str = "PAYPALISHIRING" ;
int N = 3;
printZigZagConcat(str, N);
}
}
|
Javascript
<script>
const printZigZagConcat = (str, n) => {
if (n == 1) {
document.write(`${str}<br/>`);
}
let res = "" ;
let arr = new Array(n).fill( "" );
let down = false ;
let row = 0;
for (let i = 0; i < str.length; i++) {
arr[row] += str[i];
if (row == n - 1) {
down = false ;
}
if (row == 0) {
down = true ;
}
if (!down)
row--;
else
row++;
}
for (let i = 0; i < n; i++) {
document.write(arr[i]);
}
}
let str = "PAYPALISHIRING" ;
let N = 3;
printZigZagConcat(str, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...