Zigzag String, is a Strings related problem and in this post we will see how we can solve this challenge in C++

Zigzag StringBookmark Suggest Edit 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 Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows); convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR" ini: j = 0 and direction = DOWN make a vector of string s[row] start traversing the given string: go down till you reach row-1

  1. At each step add the current char to that row
  2. when we reach row-1: then make direction as UP and --j else

++j

go up till you reach 0:

  1. At each step add the current char to that row
  2. when we reach 0: then make direction as DOWN and ++j else

--j when the string doesn't req. more than 1 row make a vector of different rows if going down if currently at the bottom change direction and start moving up when at the top row,make the direction as down and start going down copy string row-wise

Please check the main.cpp snippet for the solution.

This solution originally posted at: Github by @susantabiswas