Zigzag Conversion - Problem

Imagine you're an ancient scribe trying to encode a secret message! The Zigzag Conversion problem asks you to write a string in a zigzag pattern across multiple rows, then read it line by line to create an encoded string.

Given a string s and a number of rows numRows, you need to:

  1. Write the string in a zigzag pattern going down and up alternately
  2. Read line by line from top to bottom to get the final result

Example: The string "PAYPALISHIRING" with numRows = 3 becomes:

P   A   H   N
A P L S I I G  
Y   I   R

Reading line by line gives us: "PAHNAPLSIIGYIR"

Think of it like writing on lined paper while riding a roller coaster - your pen goes down the lines, then back up, creating this distinctive zigzag pattern!

Input & Output

example_1.py โ€” Basic Zigzag
$ Input: s = "PAYPALISHIRING", numRows = 3
โ€บ Output: "PAHNAPLSIIGYIR"
๐Ÿ’ก Note: The string is written in zigzag pattern across 3 rows: P-A-H-N on row 0, A-P-L-S-I-I-G on row 1, and Y-I-R on row 2. Reading line by line gives us the result.
example_2.py โ€” Four Rows
$ Input: s = "PAYPALISHIRING", numRows = 4
โ€บ Output: "PINALSIGYAHRPI"
๐Ÿ’ก Note: With 4 rows, the pattern becomes: P-I-N on row 0, A-L-S-I-G on row 1, Y-A-H-R on row 2, and P-I on row 3. The zigzag goes down 4 rows then up 3 rows repeatedly.
example_3.py โ€” Edge Case
$ Input: s = "A", numRows = 1
โ€บ Output: "A"
๐Ÿ’ก Note: When numRows is 1, there's no zigzag pattern possible, so we return the original string unchanged.

Constraints

  • 1 โ‰ค s.length โ‰ค 1000
  • s consists of English letters (a-z, A-Z), ',', and '.'
  • 1 โ‰ค numRows โ‰ค 1000

Visualization

Tap to expand
Zigzag Pattern: Mathematical ApproachCycle Pattern (numRows=3, cycle_len=4):Position: 0 1 2 3 4 5 6 7 8 9 10 11 12 13Char: P A Y A P L I S H I R I N GRow: 0 1 2 1 0 1 2 1 0 1 2 1 0 1Cycle: 0 1 2 3 0 1 2 3 0 1 2 3 0 1Row Calculation Formula:cycle_pos = index % cycle_lenif cycle_pos < numRows: row = cycle_poselse: row = numRows - 1 - (cycle_pos - numRows + 1)Example Calculations:P (pos 0): 0 % 4 = 0 โ†’ row 0A (pos 1): 1 % 4 = 1 โ†’ row 1Y (pos 2): 2 % 4 = 2 โ†’ row 2A (pos 3): 3 % 4 = 3 โ†’ row 1P (pos 4): 4 % 4 = 0 โ†’ row 0Final Result:Row 0: P + A + H + N = "PAHN"Row 1: A + P + L + S + I + I + G = "APLSIIG"Row 2: Y + I + R = "YIR"Result: "PAHN" + "APLSIIG" + "YIR" = "PAHNAPLSIIGYIR"
Understanding the Visualization
1
Discover the Cycle
Every (2*numRows-2) characters, the pattern repeats. For 3 rows: cycle = 4 characters
2
Map Positions
Character position % cycle_length tells us where we are in the pattern
3
Calculate Row
Use simple formulas to convert cycle position to target row
4
Build Result
Collect characters by row, then concatenate for the final answer
Key Takeaway
๐ŸŽฏ Key Insight: The zigzag pattern is actually a predictable mathematical cycle! By recognizing that characters repeat their row positions every (2*numRows-2) positions, we can skip the simulation entirely and directly compute where each character belongs.
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22
38.4K Views
Medium Frequency
~15 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen