> For the complete documentation index, see [llms.txt](https://code-snippets.hbamithkumara.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-snippets.hbamithkumara.com/leetcode/problems/1-100/zigzag-conversion.md).

# 6. ZigZag Conversion

### Description

&#x20;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)

<div align="left"><img src="/files/-MFR1U1gN3gSV-MtLqty" alt=""></div>

&#x20;And then read line by line: `"PAHNAPLSIIGYIR"`

### Constraints

### Approach

### Links

* [GeeksforGeeks](https://www.geeksforgeeks.org/print-concatenation-of-zig-zag-string-form-in-n-rows/)
* [Leetcode](https://leetcode.com/problems/zigzag-conversion/)
* [ProgramCreek](https://www.programcreek.com/2014/05/leetcode-zigzag-conversion-java/)
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}
**Input:** s = "PAYPALISHIRING", numRows = 3

**Output:** "PAHNAPLSIIGYIR"

**Explanation:**

<div align="left"><img src="/files/-MFR36rAopkQMPnw-3Xq" alt=""></div>
{% endtab %}

{% tab title="Example 2" %}
**Input:** s = "PAYPALISHIRING", numRows = 4

**Output:** "PINALSIGYAHRPI"

**Explanation:**

<div align="left"><img src="/files/-MFR2oY6Z0Boi_EH_9dQ" alt=""></div>
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="Solution 1" %}

```java
/**
 * Time complexity : 
 * Space complexity : 
 */

class Solution {
    public String convert(String s, int numRows) {
        if(s.length() == 1 || numRows <= 1) return s;
        int row = 0;
        int n = s.length();
        boolean down = true;
        
        String[] arr = new String[n];
        Arrays.fill(arr, "");
        
        for(int i = 0; i < n; i++) {
            arr[row] += s.charAt(i);
            
            if(row == numRows-1) {
                down = false;
            } else if(row == 0) {
                down = true;
            }
            if(down) {
                row++;
            } else {
                row--;
            }
        }
        return String.join("", arr);
    }
}
```

{% endtab %}

{% tab title="Solution 2" %}

```java
/**
 * Time complexity : 
 * Space complexity : 
 */

class Solution {
    public String convert(String s, int numRows) {
        if(s.length() == 1 || numRows <= 1) return s;
        int row = 0;
        int n = s.length();
        boolean down = false;
        
        String[] arr = new String[numRows];
        Arrays.fill(arr, "");
        
        for(char ch: s.toCharArray()) {
            arr[row] += ch;
            if(row == numRows-1 || row == 0) down = !down;
            row += (down ? 1: -1);
        }
        
        return String.join("", arr);
    }
}
```

{% endtab %}

{% tab title="Solution 3" %}

```java
/**
 * Time complexity : 
 * Space complexity : 
 */

class Solution {
    public String convert(String s, int numRows) {
        int n = s.length();
        if(n == 1 || numRows <= 1 || numRows > n) return s;
        int interval = (2 * numRows) - 2;
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < numRows; i++) {
          int step = interval - (2 * i);
          for(int j = i; j < n; j += interval) {
              sb.append(s.charAt(j));
              if(step > 0 && step < interval && (j+step) < n) {
                  sb.append(s.charAt(j+step));
              }
          }
        }
        return sb.toString();
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*
