Transpose a String Grid
Problem statement
You are given a string text and two integers rows and cols. Write text into a grid with rows rows and cols columns, row by row: the first cols characters form row 0, the next cols characters form row 1, and so on.
Transpose the grid: column c of the grid, read from top to bottom, becomes row c of the new grid. The new grid has cols rows and rows columns.
Return the characters of the new grid joined row by row into one string. Every character is one cell, including spaces and punctuation.
Function
transposeString(text: String, rows: int, cols: int) → StringExamples
Example 1
text = "abcdefghi"rows = 3cols = 3return = "adgbehcfi"Row by row, the grid is abc, def, ghi. Its first column read from top to bottom is adg, the second is beh, and the third is cfi. These become the rows of the transposed grid, so the answer is adgbehcfi.
Example 2
text = "abcdef"rows = 2cols = 3return = "adbecf"The grid has 2 rows of 3: abc and def. The transposed grid has 3 rows of 2: ad, be and cf. Joined, they give adbecf.
Example 3
text = "to-do!"rows = 3cols = 2return = "t-ood!"The grid has 3 rows of 2: to, -d and o!. Its columns read from top to bottom are t-o and od!. The hyphen and the exclamation mark move like any other character, so the answer is t-ood!.
Constraints
1 <= text.length <= 10^41 <= rows <= 10^41 <= cols <= 10^4rows * cols == text.lengthtextcontains only printable ASCII characters (codes 32 to 126).