MediumPythonJavaScriptTypeScript
Strings — Zigzag Conversion
AlgorithmsStringsSimulation
solve(s, numRows) — write s in a zigzag pattern on numRows rows, going down then
diagonally up, then read the rows one after another. With "PAYPALISHIRING" on 3 rows:
P A H N
A P L S I I G
Y I Rthe answer is "PAHNAPLSIIGYIR". Do not draw the grid: keep one string per row, and move a
row index down until it hits the last row, then up until it hits row 0, flipping direction at
the edges. With a single row (or at least as many rows as characters) the string is unchanged.
Examples
solve("PAYPALISHIRING", 3) → "PAHNAPLSIIGYIR"
solve("PAYPALISHIRING", 4) → "PINALSIGYAHRPI"
solve("AB", 1) → "AB"Sample tests
Test #1Three rows
Input: ["PAYPALISHIRING",3]
Output: "PAHNAPLSIIGYIR"
Test #2Four rows
Input: ["PAYPALISHIRING",4]
Output: "PINALSIGYAHRPI"
Test #3Single character
Input: ["A",1]
Output: "A"