Array ( [0] => [1] => questions [2] => Strings [3] => Zig-Zag-concatenation.-- ) Strings | Zig Zag concatenation. | THE INQUISITIVE





Zig Zag concatenation.

LEVEL:Beginner

Description

Given a string N and value k. You have to convert the given string into k rows continuously and apply zig zag fashion on those k rows and combine those k rows.

Input Format

First line contains the string N. Next line contains the value K.

Output Format

Print the string with zig zag fashion.


Example 1:

Input
abcdefgh
2
Output
acegbdfh
Example 2:

Input
coding
2
Output
cdnoig		
Example 3:

Input
educate
3
Output
ecedaut

oops

Login to see Discussion




Approach

step-i) create a empty string answer
step-ii) assign 0 to n
step-iii) assign i to j
step-iv) append character at j in string to answer and increment j by N values
step-v) repeat step iv till j is less than length of string
step-vi) increment i by one value
step-vii)repeat steps iii to vi till i is less than N
step-viii) return answer

Time Complexity: O(n)
Space Complexity: O(n)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution