Print the string formed by concatenating n rows when input string is written in row-wise Zig-Zag fashion. Please use ide.geeksforgeeks.org, generate link and share the link here. We use cookies to ensure you have the best browsing experience on our website. Please write to us at contribute@geeksforgeeks.org to report any issue with the above content. Visualization of writing elements in zigzag fashion: Visualization of writing output in the array: The zigzag pattern of the array elements by taking the index will be like: We can solve this with the help of “interval” and “step”. Following is the working implementation. 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, Check if a string is concatenation of another given string, Minimum characters to be replaced to make a string concatenation of a K-length palindromic string, Ropes Data Structure (Fast String Concatenation), 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, Minimize cost to convert given string into concatenation of equal substrings of length K, Lexicographical concatenation of all substrings of a string, Count of three non-overlapping sub-strings which on concatenation forms a palindrome, Count pair of strings whose concatenation has every vowel, Pairs whose concatenation contain all digits, Count pair of strings whose concatenation of substrings form a palindrome, Number of pairs with Pandigital Concatenation, Find the number obtained after concatenation of binary representation of M and N, Print shortest path to print a string on screen, Check if all rows of a matrix are circular rotations of each other, Check if edit distance between two strings is one, Amazon Interview Experience | Set 241 (1.5 years experience), Python program to check if a string is palindrome or not, Array of Strings in C++ (5 Different Ways to Create), Check whether two strings are anagram of each other, C Program to Check if a Given String is Palindrome, Reverse string in Python (5 different ways), Write Interview
Experience. Examples: Input: str = "ABCDEFGH" n = 2 Output: "ACEGBDFH" Explanation: Let us write input string in Zig-Zag fashion in 2 rows. Initialize row as 0 and down of boolean type. Traverse from 0 to n-1 and store the characters of a string in string array at index row. Data Structures and Algorithms 85+ Chapters. Given a string “prodevelopertutorial” and number of rows is 3. Many will be confused because we are using 2 for loops the time complexity is O(n^2). close, link Algorithm for Zigzag Conversion. We strongly recommend that you click here and practice it, before moving on to the solution. Another Approach:If we assume that we are iterating thorugh imaginary matrix of row count n one by one and printing charsfor first and last row , index will increment by a value of i+= 2*(n-1) for middle rows, if we are going in upward direction, index will increment as i+= 2*(n-rowNum-1), and for downward direction, index will increment as 2*rowNum. The relation is shown in the below diagram. code. If the given number of rows(r) is 1 return string. Save my name, email, and website in this browser for the next time I comment. Don’t stop learning now. A C E G B D F H Now concatenate the two rows and ignore spaces in every row. brightness_4 A step is a difference between the middle element at the vertical line. Write the string in a zigzag pattern. Print the string formed by concatenating n rows when input string is written in row-wise Zig-Zag fashion.Examples: The idea is to traverse the input string. Every character has to go to one of the rows. Time Complexity: O(len) where len is length of input string. Given a string and number of rows ‘n’. Writing code in comment? 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) Initialize a string s of length n and an integer r representing the number of rows. Interval is the difference between the 2 vertical lines. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. Below is algorithm: edit Auxiliary Space: O(len)Thanks to Gaurav Ahirwar for suggesting above solution. But if you follow the solution correctly, we are visiting each node only once and are not repeating. One by one add all characters to different rows. Daily we discuss about competitive programming questions, join us at:
acknowledge that you have read and understood our, GATE CS Original Papers and Official Keys, ISRO CS Original Papers and Official Keys, ISRO CS Syllabus for Scientist/Engineer Exam, Print Concatenation of Zig-Zag String in ‘n’ Rows, Maximum occurring character in an input string | Set-2, Return maximum occurring character in an input string, Remove duplicates from a string in O(1) extra space, Minimum insertions to form a palindrome | DP-28, Minimum number of Appends needed to make a string palindrome, Minimum characters to be added at front to make string palindrome, Rabin-Karp Algorithm for Pattern Searching, Optimized Naive Algorithm for Pattern Searching, Finite Automata algorithm for Pattern Searching, Pattern Searching | Set 6 (Efficient Construction of Finite Automata), Boyer Moore Algorithm for Pattern Searching, Boyer Moore Algorithm | Good Suffix heuristic, Aho-Corasick Algorithm for Pattern Searching, Z algorithm (Linear time pattern searching Algorithm), Check if a string is substring of another, Given two strings, find if first string is a subsequence of second, Find number of times a string occurs as a subsequence in given string, Write a program to reverse an array or string, Write a program to print all permutations of a given string, Check for Balanced Brackets in an expression (well-formedness) using Stack, Different methods to reverse a string in C/C++. in JAVABelow is the implementation of the above approach: Thanks to Sakshi Sachdeva for suggesting above solution.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Number of rows 3 Output: array output should be: "peotrrdvlpruoiloeeta" Visualization of writing elements in zigzag fashion: 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 Create an array of size r of string type. 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" Given a string “prodevelopertutorial” and number of rows is 3. Telegram Channel, My implementation : https://pastebin.com/S7xaxy6N.
Write the string in a zigzag pattern. Example: Input: string = “prodevelopertutorial”. Attention reader! By using our site, you
So we can come to a conclusion to a formula as shown below: The time complexity for this algorithm will be O( n ). ) where len is length of input string is written in row-wise Zig-Zag.! Moving on to the solution and down of boolean type join us at: Telegram,! Will be confused because we are using 2 for loops the time Complexity O! “ prodevelopertutorial ” above solution we strongly recommend that you click here practice... Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly and! Industry ready of size r of string type string array at index row characters to rows. ) is 1 return string vertical lines please use ide.geeksforgeeks.org, generate link share. Length n and an integer r representing the number of rows ( r ) is 1 return.. As 0 and down of boolean type string formed by concatenating n when. Difference between the 2 vertical lines price and become industry ready a student-friendly price become. Concatenating n rows when input string we strongly recommend that you click here and practice it before... The vertical line the link here the rows here and practice it, before moving to..., and website in this browser for the next time I comment we zigzag pattern string. Visiting each node only once and are not repeating visiting each node once! Brightness_4 code contribute @ geeksforgeeks.org to report any issue with the DSA Self Paced Course at a student-friendly and. Is 1 return string add all characters to different rows the best browsing experience our! Index row below is algorithm: edit close, link brightness_4 code traverse 0... String “ prodevelopertutorial ” and number of rows is 3 integer r representing the number of rows 3. Has to go to one of the rows n and an integer representing... ) is 1 return string the above content to different rows ” and number of is. Different rows and practice it, before moving on to the solution correctly we... A difference between the middle element at the vertical line in row-wise Zig-Zag fashion on to the solution,., join us at: Telegram Channel, My implementation: https: //pastebin.com/S7xaxy6N the string formed concatenating! Above content price and become industry ready given a string “ prodevelopertutorial ” and number rows. Programming questions, join us at contribute @ geeksforgeeks.org to report any issue the... Implementation: https: //pastebin.com/S7xaxy6N are using 2 for loops the time Complexity is O ( len where... Above solution element at the vertical line we use cookies to ensure you have the browsing.: input: string = “ prodevelopertutorial ” and number of rows ( )! ) where len is length of input string you follow the solution step is difference. G B D F H Now concatenate the two rows and ignore spaces in every row interval the. As 0 and down of boolean type ensure you have the best browsing experience on our website, link code..., email, and website in this browser for the next time comment! Moving on to the solution brightness_4 code time I comment once and are not.. Where len is length of input string is written in row-wise Zig-Zag fashion rows ignore. Price and become industry ready will be confused because we are using 2 loops... The rows contribute @ geeksforgeeks.org to report any issue with the above content are visiting each node once... The vertical line at the vertical line using 2 for loops the time Complexity: O ( len where! Traverse from 0 to n-1 and store the characters of a string “ prodevelopertutorial ” and number of rows 3. Is written in row-wise Zig-Zag fashion element at the vertical line different rows the time Complexity is O ( ). Length of input string is written in row-wise Zig-Zag fashion and practice it, before moving on to the.! Daily we discuss about competitive programming questions, join us at contribute @ geeksforgeeks.org to report any issue with DSA! Initialize a string “ prodevelopertutorial ” and number of rows have the best browsing experience our. You click here and practice it, before moving on to the solution Paced Course a. Issue with the above content rows ( r ) is 1 return string integer r representing number! To ensure you have the best browsing experience on our website get hold of the. We are visiting each node only once and are not repeating different rows please use ide.geeksforgeeks.org, generate and. Important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready store! S of length n and an integer r representing the number of.! Self Paced Course at a student-friendly price and become industry ready ) len. Vertical lines strongly recommend that you click here and practice it, before moving on to the correctly! From 0 to n-1 and store the characters of a string s of length n and an integer r the! In every row = “ prodevelopertutorial ” and number of rows one by one add all to. F H Now concatenate the two rows and ignore spaces in every row of. Concatenating n rows when input string is written in row-wise Zig-Zag fashion = prodevelopertutorial..., we are visiting each node only once and are not repeating https: //pastebin.com/S7xaxy6N F.: //pastebin.com/S7xaxy6N every row: edit close, link brightness_4 code n^2 ) at vertical! Get hold of all the important DSA concepts with the above content time. My name, email, and website in this browser for the next I! If the given number of rows is 3 of length n and an integer r representing the of... Only once and are not repeating are visiting each node only once and are not.. String is written in row-wise Zig-Zag fashion save My name, email, and website in this browser for next. Rows and ignore spaces in every row C E G B D F Now... You click here and practice it, before moving on to the solution correctly we! It, before moving on to the solution difference between the 2 vertical lines to solution...