<p>The string <code>&quot;PAYPALISHIRING&quot;</code> 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>

<pre>
P   A   H   N
A P L S I I G
Y   I   R
</pre>

<p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p>

<p>Write the code that will take a string and make this conversion given a number of rows:</p>

<pre>
string convert(string s, int numRows);
</pre>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3
<strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4
<strong>Output:</strong> &quot;PINALSIGYAHRPI&quot;
<strong>Explanation:</strong>
P     I    N
A   L S  I G
Y A   H R
P     I
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;A&quot;, numRows = 1
<strong>Output:</strong> &quot;A&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li>
	<li><code>1 &lt;= numRows &lt;= 1000</code></li>
</ul>

#include <iostream>
#include <string>
using namespace std;

class Solution {
public:
    string convert(string s, int numRows) {
        // Edge case: if numRows is 1, return the string as no conversion is needed
        if (numRows == 1) return s;
        
        // Define 'f' as the length of the string 's'
        int f = s.length();
        
        // Dynamically allocate arrays
        char** arr = new char*[1];       // Only one row needed
        arr[0] = new char[f];
        char** sr = new char*[numRows];  // Zigzag pattern array
        for (int i = 0; i < numRows; ++i) {
            sr[i] = new char[f]();  // Initialize all elements to 0
        }

        // Fill the single-row array
        int index = 0;
        for (int j = 0; j < f; j++) {
            arr[0][j] = s[index++];
        }

        // Zigzag conversion logic
        int j = 0, i = 0, m = 0;
        bool goingDown = false;  // Direction flag
        
        for (int k = 0; k < f; ++k) {
            sr[i][j] = arr[0][k];
            
            // Change direction when reaching the top or bottom row
            if (i == 0 || i == numRows - 1) {
                goingDown = !goingDown;
            }
            
            // Move either down or up depending on the direction
            if (goingDown) {
                i++;  // Move down the rows
            } else {
                i--;  // Move up the rows
                j++;  // Move horizontally right when going up
            }
        }

        // Read the zigzag pattern row by row to form the result
        string result;
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < f; j++) {
                if (sr[i][j] != 0) {  // Avoid appending empty cells
                    result += sr[i][j];
                }
            }
        }

        // Deallocate memory
        delete[] arr[0];
        delete[] arr;
        for (int i = 0; i < numRows; ++i) {
            delete[] sr[i];
        }
        delete[] sr;

        return result;
    }
};

41056