mediumStringArrays & Hashing

Zigzag Conversion

mediumTime: O(n)Space: O(n)

Signals to notice

write string in zigzag across rowsread row by rowoscillating row index

Brute force first

Build a 2D grid — O(n × numRows) space.

The key insight

Array of strings (one per row). Walk through string assigning each char to its row. Row bounces: 0→numRows-1→0→... O(n).

What must stay true

The row index oscillates between 0 and numRows-1. Each character appends to its row's string. Concatenating all rows gives the result.

Easy way to go wrong

Edge case: numRows = 1 → return the original string. No zigzag needed.

Arrays & Hashing Pattern