dsa · medium
Zigzag Conversion
Write s in a zigzag across num_rows rows, going down then diagonally up, then down again. Read the characters row by row and return that concatenation.
Arguments
s— the source stringnum_rows— how many rows the zigzag uses
Example
s = "PAYPALISHIRING", num_rows = 3 writes:
P A H N A P L S I I G Y I R
Reading rows: "PAHNAPLSIIGYIR".
Constraints
1 <= s.length <= 1000 1 <= num_rows <= 1000 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: "PAYPALISHIRING" 3 Expected: PAHNAPLSIIGYIR
Example 2
Input: "PAYPALISHIRING" 4 Expected: PINALSIGYAHRPI
Example 3
Input: "A" 1 Expected: A