Zigzag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows.
For example, with numRows = 3, the pattern is:
P A H N
A P L S I I G
Y I R
After reading the pattern row by row, the converted string becomes "PAHNAPLSIIGYIR".
Given a string s and an integer numRows, return the string obtained after writing the characters in a zigzag pattern and then reading them row by row.
Example 1
Input
PAYPALISHIRING 3
Output
PAHNAPLSIIGYIR
Explanation
Writing the string in a zigzag pattern with 3 rows and reading row by row produces "PAHNAPLSIIGYIR".
Example 2
Input
PAYPALISHIRING 4
Output
PINALSIGYAHRPI
Explanation
After arranging the characters in 4 zigzag rows and reading row by row, the resulting string is "PINALSIGYAHRPI".
Constraints
Hints:
Hint 1
Simulate writing the characters row by row while moving downward and then upward repeatedly.
Hint 2
Maintain a string for each row. Append each character to its corresponding row, then concatenate all rows to form the final answer.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 4, 2026
Last updated
Aug 5, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output