The string “KICKSTART” can be written in a vertical zig-zag fashion in a given number of rows:
K S T
I K T R
C A
Reading this string line by line gives us the resultant string as “KSTIKTRCA”.
You are given a string and the number of rows you can occupy. Write a code that returns the line by line representation of the string when it is written in the vertical zig-zag fashion in the given number of rows.
Input: INTERVIEW, 4
Output: IINVETRWE
The given string can be written in a zig-zag fashion in 4 rows as follows:
I I
N V E
T R W
E
Input: KICKSTART, 1
Output: KICKSTART
• 1
• 1
• The string may contain the following ASCII characters only: a..z, A..Z, 0..9
We provided one solution.
Throughout this editorial, we will refer to the input string as str and the given number of rows as num_of_rows.
We will maintain the contents of each line of the zig-zag pattern separately and finally, to generate our output string, we will iterate through all the lines and will keep adding the contents of those lines to our resultant string.
So, the idea is to build an array of strings of size num_of_rows (Let us call this array as lines) where lines[i] will represent the contents of the i-th line.
Let’s say, we have been given str = “KICKSTART” and num_of_rows = 3.
Now, this string will be written in a zig-zag fashion as follows:
K S T
I K T R
C A
So, our lines array should look like:
lines[] = {“KST”, “IKTR”, “CA”}.
Finally, we will build our resultant string as lines[0] + lines[1] + lines[2] which will be “KSTIKTRCA” for this example.
So, the question arises is how will we build the array lines?
The idea is quite simple and very analogous to how we manually built our result. Let us try to simulate that process. We will iterate the input string from left to right and add the characters of it one by one to the appropriate position of our lines array.
We will keep a track of the index of the string we are on as well as of the current line.
So, we need two things as of now:
● Current index of the string str. Let us call this string_index.
● Index of the row in array lines we are currently filling. Let us call this current_row.
Also, as you might have observed, at some instances of time, we are moving downwards through the array lines while at the other instances, we are moving upwards.
Therefore, we also need to keep a track of the direction we’re moving in. That is, whether we are moving upwards or downwards.
Let us keep a Boolean variable going_down for this purpose.
Here, going_down = True will mean that we are moving down the rows and vice-versa.
Now, we are almost done and the only thing we’re left with, is to fill the array lines.
We will iterate through the entire string (ie, till string_index < string_length) and will add str[string_index] to lines[current_row].
After each iteration, we will update current_row = current_row + 1 if going_down = True. Else, we will update current_row = current_row - 1.
Also, as you might have observed, we reverse our direction when we are at one of the end rows. That is, either when current_row = 0 or current_row = num_of_rows - 1.
Now, since we are done with all the prerequisites, let us see how this approach will fill the array lines for the following example:
str = “KICKSTART”, num_of_rows = 3.
The transitions will look like:
{“K”, “”, “”} Move DOWN
{“K”, “I”, “”} Move DOWN
{“K”, “I”, “C”} Move UP
{“K”, “IK”, “C”} Move UP
{“KS”, “IK”, “C”} Move DOWN
{“KS”, “IKT”, “C”} Move DOWN
{“KS”, “IKT”, “CA”} Move UP
{“KS”, “IKTR”, “CA”} Move UP
{“KST”, “IKTR”, “CA”} Move DOWN
Finally, as pointed out before, we generate our result as:
lines[0] + lines[1] + lines[2] = “KSTIKTRCA”.
The above approach looks fine. But does it work for the case when num_of_rows is 1? No it does not. In this case, we do not have to move up and down the rows. Rather, all the work has to be done in a single row itself. So, we will have to handle this edge case separately to return the input string itself in case when num_of_rows = 1.
O(length of str).
Since we are iterating through each character in the string only once.
O(num_of_rows + length of str).
We created the array lines of size num_of_rows. This requires O(num_of_rows) auxiliary space. We also stored all the characters of the input string in this array. This requires O(length of str) space.
O(num_of_rows + length of str).
For space complexity, we also include the space occupied by input and output strings. So, the space complexity of this would be O(num_of_rows + 3*(length of str)) = O(num_of_rows + length of str).
The string “KICKSTART” can be written in a vertical zig-zag fashion in a given number of rows:
K S T
I K T R
C A
Reading this string line by line gives us the resultant string as “KSTIKTRCA”.
You are given a string and the number of rows you can occupy. Write a code that returns the line by line representation of the string when it is written in the vertical zig-zag fashion in the given number of rows.
Input: INTERVIEW, 4
Output: IINVETRWE
The given string can be written in a zig-zag fashion in 4 rows as follows:
I I
N V E
T R W
E
Input: KICKSTART, 1
Output: KICKSTART
• 1
• 1
• The string may contain the following ASCII characters only: a..z, A..Z, 0..9
We provided one solution.
Throughout this editorial, we will refer to the input string as str and the given number of rows as num_of_rows.
We will maintain the contents of each line of the zig-zag pattern separately and finally, to generate our output string, we will iterate through all the lines and will keep adding the contents of those lines to our resultant string.
So, the idea is to build an array of strings of size num_of_rows (Let us call this array as lines) where lines[i] will represent the contents of the i-th line.
Let’s say, we have been given str = “KICKSTART” and num_of_rows = 3.
Now, this string will be written in a zig-zag fashion as follows:
K S T
I K T R
C A
So, our lines array should look like:
lines[] = {“KST”, “IKTR”, “CA”}.
Finally, we will build our resultant string as lines[0] + lines[1] + lines[2] which will be “KSTIKTRCA” for this example.
So, the question arises is how will we build the array lines?
The idea is quite simple and very analogous to how we manually built our result. Let us try to simulate that process. We will iterate the input string from left to right and add the characters of it one by one to the appropriate position of our lines array.
We will keep a track of the index of the string we are on as well as of the current line.
So, we need two things as of now:
● Current index of the string str. Let us call this string_index.
● Index of the row in array lines we are currently filling. Let us call this current_row.
Also, as you might have observed, at some instances of time, we are moving downwards through the array lines while at the other instances, we are moving upwards.
Therefore, we also need to keep a track of the direction we’re moving in. That is, whether we are moving upwards or downwards.
Let us keep a Boolean variable going_down for this purpose.
Here, going_down = True will mean that we are moving down the rows and vice-versa.
Now, we are almost done and the only thing we’re left with, is to fill the array lines.
We will iterate through the entire string (ie, till string_index < string_length) and will add str[string_index] to lines[current_row].
After each iteration, we will update current_row = current_row + 1 if going_down = True. Else, we will update current_row = current_row - 1.
Also, as you might have observed, we reverse our direction when we are at one of the end rows. That is, either when current_row = 0 or current_row = num_of_rows - 1.
Now, since we are done with all the prerequisites, let us see how this approach will fill the array lines for the following example:
str = “KICKSTART”, num_of_rows = 3.
The transitions will look like:
{“K”, “”, “”} Move DOWN
{“K”, “I”, “”} Move DOWN
{“K”, “I”, “C”} Move UP
{“K”, “IK”, “C”} Move UP
{“KS”, “IK”, “C”} Move DOWN
{“KS”, “IKT”, “C”} Move DOWN
{“KS”, “IKT”, “CA”} Move UP
{“KS”, “IKTR”, “CA”} Move UP
{“KST”, “IKTR”, “CA”} Move DOWN
Finally, as pointed out before, we generate our result as:
lines[0] + lines[1] + lines[2] = “KSTIKTRCA”.
The above approach looks fine. But does it work for the case when num_of_rows is 1? No it does not. In this case, we do not have to move up and down the rows. Rather, all the work has to be done in a single row itself. So, we will have to handle this edge case separately to return the input string itself in case when num_of_rows = 1.
O(length of str).
Since we are iterating through each character in the string only once.
O(num_of_rows + length of str).
We created the array lines of size num_of_rows. This requires O(num_of_rows) auxiliary space. We also stored all the characters of the input string in this array. This requires O(length of str) space.
O(num_of_rows + length of str).
For space complexity, we also include the space occupied by input and output strings. So, the space complexity of this would be O(num_of_rows + 3*(length of str)) = O(num_of_rows + length of str).
Attend our free webinar to amp up your career and get the salary you deserve.