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.
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:
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
What is the zigzag pattern of the string “Educative” with the number of rows defined as four?
Educative
Eidtvuaec
Dvuaecit
Evitacdue
Here are the basic steps we need to follow to solve this problem in code:
Handle the base cases.
Create a list of empty strings with the length of the rows defined.
Create a variable to keep track of the row index.
Create a variable to keep track of the movement direction (1
and -1
).
Create a variable to output at the end and set it to an empty string.
Loop through the input string.
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).
Add the direction value to the row index.
Append the character to the list at the row index.
Output the resultant string.
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 srow_list = [''] * rowscurr = 0direction = 1for c in s:row_list[curr] += cif curr == 0:direction = 1elif curr == rows - 1:direction = -1curr += directionresult = ''.join(row_list)return resultinputSTR = "EXAMPLERUN"num = 4output = 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 . 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