Problem 335952 · medium · Phase 03 Linear Management & Searching

Rail Fence Rows

strings · simulation · bucketing

A rail fence cipher writes a message diagonally over k rails: the first character goes on rail 0, the next on rail 1, and so on down to rail k - 1, then back up to rail 0, and so forth. The ciphertext is obtained by reading rail 0 left to right, then rail 1, and so on. Given s and k, return the ciphertext.

Examples

Input:  s = "WEATHERREPORT", k = 3
Output: "WHETETERPRARO"
Explanation:
    W . . . H . . . E . . . T
    . E . T . E . R . P . R .
    . . A . . . R . . . O . .

Input:  s = "ABCDE", k = 2
Output: "ACEBD"

Constraints

  • 0 <= len(s) <= 10**4, 1 <= k <= 1000
  • Target: O(n) time.

Goals

  • Simulate a bouncing row index that reverses at the top and bottom rails
  • Collect characters into one bucket per rail
  • Join the buckets in rail order
Starting Python…