Given a string and number of rows ‘n’. Print the string formed by concatenating n rows when input string is written in row-wise Zig-Zag fashion.
Input: str = "ABCDEFGH" n = 2 Output: "ACEGBDFH" Explanation: Let us write input string in Zig-Zag fashion in 2 rows. A C E G B D F H Now concatenate the two rows and ignore spaces in every row. We get "ACEGBDFH" Input: str = "GEEKSFORGEEKS" n = 3 Output: GSGSEKFREKEOE Explanation: Let us write input string in Zig-Zag fashion in 3 rows. G S G S E K F R E K E O E Now concatenate the two rows and ignore spaces in every row. We get "GSGSEKFREKEOE"
We strongly recommend that you click here and practice it, before moving on to the solution.
The idea is to traverse the input string. Every character has to go to one of the rows. One by one add all characters to different rows. Below is algorithm:
1) Create an array of n strings, arr[n] 2) Initialize direction as "down" and row as 0. The direction indicates whether we need to move up or down in rows. 3) Traverse the input string, do following for every character. a) Append current character to string of current row. b) If row number is n-1, then change direction to 'up' c) If row number is 0, then change direction to 'down' d) If direction is 'down', do row++. Else do row--. 4) One by one print all strings of arr.
Below is implementation of above idea.
Time Complexity: O(len) where len is length of input string.
Auxiliary Space: O(len)
Thanks to Gaurav Ahirwar for suggesting above solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Ropes Data Structure (Fast String Concatenation)
- Lexicographical concatenation of all substrings of a string
- Longest palindromic String formed using concatenation of given strings in any order
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Count of pairs of strings whose concatenation forms a palindromic string
- Concatenation of two strings in PHP
- Number of pairs with Pandigital Concatenation
- Pairs whose concatenation contain all digits
- Count of three non-overlapping sub-strings which on concatenation forms a palindrome
- Find the number obtained after concatenation of binary representation of M and N
- Caesar Concatenation
- Count pair of strings whose concatenation of substrings form a palindrome
- Count pair of strings whose concatenation has every vowel
- Print shortest path to print a string on screen
- Check if all rows of a matrix are circular rotations of each other
- Print the longest prefix of the given string which is also the suffix of the same string
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Print string after removing all (“10” or “01”) from the binary string