All problems
0061MediumString

Engraved Track Transcript

Tracked in this browser only
Write code

Trains the technique from

LeetCode 6Zigzag Conversion

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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