Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Zigzag Conversion (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a string s and a numRows integer representing the number of rows, write the string in a zigzag pattern on the given number of rows and then read it line by line, and return the resultant string.

The zigzag pattern means writing characters in a diagonal down-and-up fashion. For example, given the string "HELLOPROGRAMMING" and 4 rows, the pattern would look like:

H     R     M
E   P O   M I
L O   G A   N
L     R     G

Reading it line by line gives us "HRMEPOMILOGANLRG".

Examples

Example 1:

  • Input: s = "HELLOPROGRAMMING", numRows = 4
  • Expected Output: "HRMEPOMILOGANLRG"
  • Explanation: The zigzag pattern is:
    H     R     M
    E   P O   M I
    L O   G A   N
    L     R     G
    
    Reading line by line gives "HRMEPOMILOGANLRG".

Example 2:

  • Input: s = "PROBLEMCHALLENGE", numRows = 5
  • Expected Output: "PHRCAEOMLGBELNLE"
  • Explanation: The zigzag pattern is:
    P       H       
    R     C A     E 
    O   M   L   G   
    B E     L N     
    L       E       
    
    Reading line by line gives "LCCETEHLDELOEGAEEN".

Example 3:

  • Input: s = "ABCDE", numRows = 2
  • Expected Output: "ACEBD"
  • Explanation: The zigzag pattern is:
    A C E
    B D
    
    Reading line by line gives "ACEBD".

Constraints:

  • 1 <= s.length <= 1000
  • s consists of English letters (lower-case and upper-case), ',' and '.'.
  • 1 <= numRows <= 1000

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible