loading

Zigzag Conversion — Step-by-Step Visualization

mediumLeetCode #6String

The string 'PAYPALISHIRING' is written in a zigzag pattern on a given number of rows. Read line by line to produce a new string. Given the string s and the number of rows numRows, return the output string.

Algorithm Pattern

Row Simulation

Key Idea

Distribute characters row by row following the zigzag pattern, then concatenate rows.

Step-by-Step Approach

  1. Create numRows empty strings
  2. Track current row and direction
  3. Append each char to current row
  4. Reverse direction at row 0 and numRows-1
  5. Concatenate all rows

Related Problems