Zigzag Conversion
Zigzag Conversion is LeetCode 6 (Medium). Write the string s in a zigzag pattern over numRows rows: straight down from row 0 to the last row, then diagonally back up to row 0, then down again, until the characters run out. Return the rows read left to right, top row first.
s has at most 1,000 characters and numRows can be anything from 1 to 1,000, so even drawing a full grid would pass; the point is to see that no grid is needed.
- 1 <= s.length <= 1000
- s consists of English letters (lower-case and upper-case), ',' and '.'.
- 1 <= numRows <= 1000
Intuition
The drawing has columns and blank cells, but the answer never uses them: reading row by row only needs which row each character lands in, in the order it arrives. So keep one growing string per row and walk through s once.
The row number moves like a ball bouncing between two walls: 0, 1, …, numRows − 1, then back up to 0, and down again. Keep a step of +1 or −1 and flip it whenever the row reaches the top or the bottom. Characters reach each row in the same left-to-right order as in the drawing, so joining the rows from top to bottom gives the answer.
When a string is laid out in some shape and then read back in another order, check whether only each character's group (here, its row) matters. If so, append to per-group buffers instead of building the shape.
Approach
Before reading on, write the row number of each character of PAYPALISHIRING for 3 rows and look at the sequence. Then decide when that sequence changes direction. Aim for O(n) time.
Return early for one row
If numRows is 1, return s as it is: a single row read left to right is the string itself.
Append, then bounce
For each character, append it to rows[cur]. If cur is 0 or numRows − 1, the walk has hit a wall, so flip step; then add step to cur. Starting with step = -1 makes the first flip, at row 0, point the walk downward.
Join the rows
Concatenate rows[0], rows[1], … in order. Each row already holds its characters in left-to-right order, because the walk visits the drawing's columns from left to right, so the join is exactly the row-by-row reading.
Code in Python, C++ and Java
s as it is.Common pitfalls
Skipping the one-row check
rows = [""] * numRows cur, step = 0, -1
if numRows == 1:
return s
rows = [""] * numRowsWith one row, row 0 is the top and the bottom at once. For s = "AB", the first character flips step to +1 and moves cur to 1, a row that does not exist, so the second append crashes with an index error.
Edge cases
numRows at least the length of s, e.g. "ABC" with 5 rowsThe walk goes straight down, one character per row, and runs out before reaching the bottom wall. Each row holds one character, so the answer is s unchanged.
numRows = 2Both rows are walls, so the step flips on every character and the rows alternate. The answer is the characters at even positions followed by those at odd positions: "ABCDE" gives "ACEBD".
Complexity
s. Each character is appended once and copied once more by the join. The row strings together hold n characters.Zigzag Conversion FAQ
Can each row be built without simulating the walk?
Yes. The pattern repeats every cycle = 2 · numRows − 2 characters. Row 0 takes indices 0, cycle, 2 · cycle, …, and the last row takes numRows − 1 plus multiples of cycle. A middle row r takes k · cycle + r and, after each of those, the diagonal character at k · cycle + cycle − r. It writes the answer directly, also in O(n).
Is Concatenation of Zig-Zag String in n Rows the same problem?
Yes. It asks for the same thing: write the string in the zigzag shape and print the rows joined together. The row-bounce walk on this page solves it unchanged.