Problem statement

https://leetcode.com/problems/zigzag-conversion/

Solution

Here you need to use definition: let us go symbol by symbol and understand which row it should go to. For example if $m = 4$, then in first row we have indexes $0 \mod 6$, in next $1, 5 \mod 6$, then $2, 4 \mod 6$ and finally $3\mod 6$. We can create list of empty strings first and then fill them.

Complexity

Time and space complexity is $O(n)$, where $n$ is length of string s.

Code

class Solution:
    def convert(self, s, m):
        if m == 1: return s
        rows = [""]*m
        for i in range(len(s)):
            row_num = i % (2*m - 2)
            if row_num >= m: row_num = 2*m - 2 - row_num
            rows[row_num] += s[i]
            
        return "".join(rows)