This question on LeetCode was so interesting that I initially tried to find a pattern or rule to solve it. However, after an hour of unsuccessful attempts, I decided to write an easy and straightforward solution that I came up with. Later on, I discovered this Youtube video, which presents a more elegant and cleaner solution.
Question
Solution 1
First, we initialize each item in the StringBuilder array and then we obtain the result through the following steps.
Loop for only one vertical line chars
Get back to the start row of diagonal line(like P)
Loop for only one diagonal line chars
Continue to repeat these steps until the loop reaches the last index of s. This diagram illustrates how the steps progress.