Show problems as
Classic problem #6MediumString

Zigzag Conversion

Real-world version: Engraved Track Transcript
Tracked in this browser only
Read Zigzag Conversion on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A plate engraver cuts the characters of message into rows parallel horizontal tracks, numbered 0 at the top through rows - 1 at the bottom. It stamps the characters one at a time in the order they appear.

The head starts on track 0 and drops one track after each stamp until it stamps into the bottom track; it then climbs one track after each stamp until it stamps into the top track, and it keeps swinging that way until the message runs out. Each stamp lands to the right of anything already cut into that track, so within a track the characters stay in the order they were stamped. When rows is 1 the head never leaves the top track.

The inspector reads the plate one track at a time, top track first, taking each track from left to right. Return the string that reading produces.

Examples

Example 1

Input
message = "QuietHum", rows = 2
Output
"QituueHm"

With two tracks the head alternates on every stamp, so the top track collects the characters at positions 0, 2, 4 and 6 while the bottom track collects those at 1, 3, 5 and 7. Reading the top track and then the bottom track gives QituueHm.

Example 2

Input
message = "ENGRAVETHISLABEL", rows = 4
Output
"EEANVTLBGAHSERIL"

A full swing across four tracks takes six stamps, so the head is back on the top track at positions 0, 6 and 12, giving it E, E and A. Track 1 gathers positions 1, 5, 7, 11 and 13, and the two lower tracks follow beneath.

Example 3

Input
message = "zzzz", rows = 1
Output
"zzzz"

With a single track the head never moves off it, so the plate reads exactly like the message.

Constraints

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

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def engrave_transcript(message: str, rows: int) -> str:
Java
public String engraveTranscript(String message, int rows)