FunctionRacerProblemsRace

Undo the Column Read

MediumStringsPython, JavaScript, TypeScript, Java, C++

Someone took a text, wrote it into a grid with k characters per row (filling row after row, so only the last row could be shorter), and then read the grid one column at a time, each column from top to bottom, leftmost column first. t is what they read. Recover and return the original text.

For example t = "hloel" with k = 2 came from "hello": its rows were he, ll and o, and its columns hlo and el. The original text is always unique, and an empty t gives "".

Example 1

t "hloel"
k 2
returns "hello"

Five characters in rows of 2 make 3 rows with a short last row, so the first column has 3 entries and the second has 2: hlo fills column one and el column two, which reads back row by row as hello.

Example 2

t "adgbecf"
k 3
returns "abcdefg"

Seven characters in rows of 3 make 3 rows; only the first column has 3 entries, the other two have 2. Handing out adgbecf column by column with those heights rebuilds abcdefg.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems