What is the zigzag conversion problem in Python?

Zigzag conversion is a common coding interview problem that employs string manipulation. We’ll be provided a string, which we’ll have to convert into a specific pattern, which leads to a zigzag arrangement of the characters. In this Answer, we’ll look at the zigzag conversion problem and how to solve it in Python.

Problem statement

Suppose we have a string along with a positive integer called rows. We aim to convert this string into a zigzag pattern with rows (defining the number of rows in the pattern). We’ll then read the characters row by row to construct our new string. Let’s illustrate this with an example:

We use a row pointer and curr to determine the direction of movement
We use a row pointer and curr to determine the direction of movement
1 of 11

In the illustration above, we take EXAMPLERUN as our string and define four rows. We fill the zigzag, beginning vertically downwards and then moving diagonally until we reach the top again. In the end, we’re left with the string EEXLRAPUMN.

Note: The edge case for this problem will be if the rows are defined as being greater than or equal to the length of the string, or 1. In both cases, return the string itself.

Attempt the quiz below to make sure you fully understand the problem statement.

ZigZag Conversion Problem Quiz

1

What is the zigzag pattern of the string “Educative” with the number of rows defined as four?

A)

Educative

B)

Eidtvuaec

C)

Dvuaecit

D)

Evitacdue

Question 1 of 20 attempted

Steps to follow

Here are the basic steps we need to follow to solve this problem in code:

  1. Handle the base cases.

  2. Create a list of empty strings with the length of the rows defined.

  3. Create a variable to keep track of the row index.

  4. Create a variable to keep track of the movement direction (1 and -1).

  5. Create a variable to output at the end and set it to an empty string.

  6. Loop through the input string.

  7. Check if the row index is 0 (the start of the list). If so, set the direction to 1 (going downwards). If the row index is at the end of the list, set the direction to -1 (going upwards).

  8. Add the direction value to the row index.

  9. Append the character to the list at the row index.

  10. Output the resultant string.

Coding solution

The coding solution to this problem is provided below:

def zigzag(s, rows):
if rows <= 0:
return ("Please enter a valid positive number for the row count.")
if rows == 1 or rows >= len(s):
return s
row_list = [''] * rows
curr = 0
direction = 1
for c in s:
row_list[curr] += c
if curr == 0:
direction = 1
elif curr == rows - 1:
direction = -1
curr += direction
result = ''.join(row_list)
return result
inputSTR = "EXAMPLERUN"
num = 4
output = zigzag(inputSTR, num)
print(output)

The code can be explained as follows:

  • Line 1: The definition of our function zigzag, which takes two parameters, s (the input string) and rows (the number of rows in our zigzag).

  • Lines 5–6: These lines check our edge cases in which the number of rows exceeds the length of the string or if the provided number of rows is 1. In all these cases, the string itself will be returned.

  • Line 8: The row_list variable contains our new string that we’ll join and return at the end. We initialize it here.

  • Line 12: We loop through each character in the string.

  • Line 13: We add the current character c to the row specified by the curr variable.

  • Lines 14–18: If curr is at the top of the zigzag (at index 0), it changes the direction to 1, which indicates that it’s moving downwards. If the current row is at the bottom (at index rows - 1), it changes the direction to -1, which signifies upward movement. It continues in the current direction if it doesn’t fall under these conditions.

  • Lines 20–21: Join the character list into a string using the string join method and return the output.

To try other examples, we can change the input string and the number of rows in the code above. This code’s time and space complexity is O(n)O(n). To conclude, zigzag conversion is a powerful method for manipulating strings. It combines elements of encryption, formatting, and compression, which are typically used in puzzle design and other similar endeavors.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved