Untitled

Anonymous
plain_text
03/01/2026 5:33 AM
1020 B
13
Indexable
class Solution:
    def convert(self, s: str, numRows: int) -> str:
        # If only one row or string is too short, return it as is
        if numRows == 1 or numRows >= len(s):
            return s
        
        # Create a list where each element is a row
        rows = [""] * numRows
        index, step = 0, 1
        
        for char in s:
            rows[index] += char
            
            # If we reach the top row, move down; 
            # if we reach the bottom row, move up
            if index == 0:
                step = 1
            elif index == numRows - 1:
                step = -1
            
            index += step
            
        # Combine all rows into a single string
        return "".join(rows)
Editor is loading...
Leave a Comment