This problem was really frustrating!!! The problem statement is deceptively simple!
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.
The problem didn’t explain itself clearly. At first I thought the format was:
X X X X
X X X X X X
…
where the first row has one letter with three spaces in between, and the second row has one letter with one space in between.
Following this logic, I assumed odd rows had X letters and even rows had 2X-1 letters. I wrote the code based on this idea and submitted it confidently:
142 / 1158 test cases passed.
Passed 142 test cases… then it said ABCD 2 was WA?
I was completely confused. Finally, with no other options, I looked up how others interpreted the problem.
Turns out it means:
1 7
2 6 8
3 5 9
4
It should form a zigzag pattern… then output row by row… sigh.
So I started working on it, using a 2D array to store the values, changing direction, and filling empty spots with spaces… Got all kinds of RE on small test cases??? That didn’t make sense. Then I realized it might be RE on large data but just happened to be running on small data first.
Eventually I tried expanding the 2D array to 10000x10000 and it blew up with memory issues.
What to do? I almost gave up on this problem.
It all came down to my own inexperience. Later I thought of two approaches: either use a mathematical method to calculate the output order, or compress the space. I chose the latter – instead of storing a 2D map array, I stored each letter’s x, y coordinates and value, which greatly reduced the space usage.
Accepted on the first try after that!
Notes:
- Need to handle the special case of a single row.
- I was lazy and used the standard library’s sort.
Here is the code:
| |