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 s, int numRows);


&nbsp;
Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"


Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P     I    N
A   L S  I G
Y A   H R
P     I


Example 3:

Input: s = "A", numRows = 1
Output: "A"


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 1000
	s consists of English letters (lower-case and upper-case), ',' and '.'.
	1 &lt;= numRows &lt;= 1000


class Solution:
    def convert(self, s: str, numRows: int) -> str:

        # safety check to not process single row
        if numRows == 1:
            return s

        # safety check to not process strings shorter/equal than numRows
        if len(s) <= numRows:
            return s

        # safety check to not process double rows
        if numRows == 2:
            # slice every other character
            return s[0::2] + s[1::2]

        # list that stores the lines
        # add lines with initial letters
        lines: list[str] = [letter for letter in s[:numRows]]

        # positive direction goes down
        # lines are created, so it's going up
        direction: int = -1

        # track the position at which the letter will be added
        # position after bouncing off, after adding initial lines
        line_index: int = numRows - 2

        # edge indexes
        # 0 can only be reached by going up
        # numRows only by going down
        edges: set[int] = {0, numRows}

        for letter in s[numRows:]:
            # add letter at tracked index position
            lines[line_index] += letter

            # prepare index before next loop iteration
            line_index += direction

            # reaching one of the edges
            if line_index in edges:
                # change direction
                direction = -direction
                # bounce off if bottom edge
                if line_index == numRows:
                    line_index += direction * 2

        return "".join(lines)
class Solution {
    public String convert(String s, int numRows) {
        if (numRows==1)return s;
        StringBuilder builder = new StringBuilder();
        for (int i=1;i<=numRows;i++){
            int ind = i-1;
            boolean up = true;
            while (ind < s.length()){
                builder.append(s.charAt(ind));
                if (i==1){
                    ind += 2*(numRows-i);
                } else if (i==numRows){
                    ind += 2*(i-1);
                } else {
                    ind += up ? 2*(numRows-i) : 2*(i-1);
                    up=!up;
                }
            }
        }
        return builder.toString();
    }
}
class Solution {
public:
    string convert(string s, int numRows) {
        vector<vector<char>> v(numRows);
        int j=0,t=1;
        if(numRows ==1)
            return s;
        for(int i=0;i<s.size();i++)
        {
            v[j].push_back((char)s[i]);
            if(j==numRows-1 )
                t=0;
            else if(j==0)
                t=1;
            if(t)
                j++;
            else
                j--;
            
        }
        string x="";
        for(int i=0;i<numRows;i++)
        {
            for(int j=0;j<v[i].size();j++)
                x.push_back(v[i][j]);
        }
        return x;
    }
};
var convert = function(s, numRows) {
  let result = [];
  let row = 0;
  let goingUp = false;
  for (let i = 0; i < s.length; i++) {
    result[row] = (result[row] || '') + s[i]; // append letter to active row
    if (goingUp) {
      row--;
      if (row === 0) goingUp = false; // reverse direction if goingUp and reaching top
    } else {
      row++;
      if (row === numRows - 1) goingUp = true; // reverse direction after reaching bottom
    }

  }
  return result.join('');
};
Zigzag Conversion