
Leetcode Problem 6: Zigzag Conversion
Given a string s and a number of rows numRows, rearrange its characters by writing them in a zigzag pattern across numRows rows, then read the result back row by row to produce the output string.
Characters are placed diagonally top-to-bottom filling each row, then diagonally bottom-to-top, and this repeats until all characters are placed. For example, "PAYPALISHIRING" written with 3 rows looks like this:
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write a function convert(s, numRows) that performs this transformation and returns the resulting string.
,), and periods (.).function convert(s: string, numRows: number): string {
if (numRows === 1) {
return s
}
let zigZagStrings: string[] = Array(numRows).fill('')
let currentArrayIndex = 0
let direction: 1 | -1 = 1
for (let i = 1; i <= s.length; i++) {
zigZagStrings[currentArrayIndex] = zigZagStrings[currentArrayIndex] + s.charAt(i - 1)
if (currentArrayIndex === 0) {
direction = 1
}
if (currentArrayIndex === numRows - 1) {
direction = -1
}
currentArrayIndex += direction
}
return zigZagStrings.join('')
}
console.log(convert("PAYPALISHIRING", 3))