#2075
Decode the Slanted Ciphertext
specialist · 885 · lc medium +31 · verified · 50.3% accepted · 269 likes · top 38%
Description
A slanted transposition cipher encodes originalText by writing it diagonally (top-left to bottom-right) into a matrix with rows rows, padding empty cells with spaces, then reading the matrix row-by-row to form encodedText. Given encodedText and rows, decode and return originalText (which has no trailing spaces). The answer is guaranteed to be unique.
Example 1:
Input: encodedText = "ch ie pr", rows = 3
Output: "cipher"
Explanation: This is the same example described in the problem description.
Example 2:
Input: encodedText = "iveo eed l te olc", rows = 4
Output: "i love leetcode"
Explanation: The figure above denotes the matrix that was used to encode originalText.
The blue arrows show how we can find originalText from encodedText.
Example 3:
Input: encodedText = "coding", rows = 1
Output: "coding"
Explanation: Since there is only 1 row, both originalText and encodedText are the same.
Code
1
2
3