11 · Matrices & Simulation

#6 Zigzag Conversion

Mediumsolve on LeetCode ↗

frequently reported atPayPalWalmartAmazonGoogleMicrosoft

the problem

In plain words

Write the string s in a zigzag over numRows rows: go straight down the first column, then climb diagonally up and to the right until you are back on the top row, then go straight down again, and keep going. Return the text you get by reading the rows from top to bottom, each one left to right.

in s = "PAYPALISHIRING", numRows = 3

out "PAHNAPLSIIGYIR"

Row 0 holds P A H N, row 1 holds A P L S I I G, row 2 holds Y I R.

in s = "PAYPALISHIRING", numRows = 4

out "PINALSIGYAHRPI"

Rows: P I N · A L S I G · Y A H R · P I.

in s = "A", numRows = 1

out "A"

  • 1 ≤ s.length ≤ 1000
  • s has English letters (both cases), ',' and '.'
  • 1 ≤ numRows ≤ 1000

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #54
      Spiral Matrix
      Mediumin Matrices
    2. #103