# 6. ZigZag Conversion

### Approach 1: Iterate character by character

```python
class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows<=1: return s
        ans=[""]*numRows
        i=0
        for c in s:
            ans[i]+=c
            if i==numRows-1:
                d=-1
            if i==0:
                d=1
            i+=d
        return ''.join(ans)
```

Iterate over the characters one by one, while maintaining a counter for current row number. Keep incrementing the counter one by one and when you reach `numRows` start subtracting 1 each time, and append the character to the calculated index and return the join of all these rows as a string

> **Time Complexity: O(n)**
>
> **Space Complexity: O(n)**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://programming.arora-aditya.com/leetcode/string/6.-zigzag-conversion.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
