A string originalText is encoded using a slanted transposition cipher to a string encodedText with the help of a matrix having a fixed number of rowsrows.
originalText is placed first in a top-left to bottom-right manner.
The blue cells are filled first, followed by the red cells, then the yellow cells, and so on, until we reach the end of originalText. The arrow indicates the order in which the cells are filled. All empty cells are filled with ' '. The number of columns is chosen such that the rightmost column will not be empty after filling in originalText.
encodedText is then formed by appending all characters of the matrix in a row-wise fashion.
The characters in the blue cells are appended first to encodedText, then the red cells, and so on, and finally the yellow cells. The arrow indicates the order in which the cells are accessed.
For example, if originalText = "cipher" and rows = 3, then we encode it in the following manner:
The blue arrows depict how originalText is placed in the matrix, and the red arrows denote the order in which encodedText is formed. In the above example, encodedText = "ch ie pr".
Given the encoded string encodedText and number of rows rows, return the original stringoriginalText.
Note:originalTextdoes not have any trailing spaces ' '. The test cases are generated such that there is only one possible originalText.

Input: encodedText ="iveo eed l te olc", rows =4Output: "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.

Input: encodedText ="coding", rows =1Output: "coding"Explanation: Since there is only 1 row, both originalText and encodedText are the same.
The encoded text is formed by writing the original text diagonally in a matrix and then reading it row by row. To decode, we reconstruct the matrix row-wise and then read the diagonals to recover the original text.
Compute the number of columns as cols = len(encodedText) // rows.
Fill a matrix of size rows x cols with the characters from encodedText row by row.
Traverse the matrix diagonally: for each diagonal starting from the first column, collect characters from (i, j) where i is the row and j = i + d is the column.
classSolution {
public: string decodeCiphertext(string s, int rows) {
int n = s.size();
if (rows ==1) return s;
int cols = n / rows;
string ans;
for (int d =0; d < cols; ++d) {
for (int i =0; i < rows; ++i) {
int j = d + i;
if (j < cols) {
char c = s[i * cols + j];
ans += c;
}
}
}
while (!ans.empty() && ans.back() ==' ') ans.pop_back();
return ans;
}
};
classSolution {
public String decodeCiphertext(String s, int rows) {
int n = s.length();
if (rows == 1) return s;
int cols = n / rows;
StringBuilder ans =new StringBuilder();
for (int d = 0; d < cols; d++) {
for (int i = 0; i < rows; i++) {
int j = d + i;
if (j < cols) {
ans.append(s.charAt(i * cols + j));
}
}
}
// Remove trailing spacesint len = ans.length();
while (len > 0 && ans.charAt(len - 1) ==' ') {
ans.setLength(--len);
}
return ans.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
fundecodeCiphertext(s: String, rows: Int): String {
if (rows ==1) return s
val n = s.length
val cols = n / rows
val ans = StringBuilder()
for (d in0 until cols) {
for (i in0 until rows) {
val j = d + i
if (j < cols) {
ans.append(s[i * cols + j])
}
}
}
return ans.trimEnd().toString()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defdecodeCiphertext(self, s: str, rows: int) -> str:
if rows ==1:
return s
n: int = len(s)
cols: int = n // rows
ans: list[str] = []
for d in range(cols):
for i in range(rows):
j = d + i
if j < cols:
ans.append(s[i * cols + j])
return''.join(ans).rstrip()
impl Solution {
pubfndecode_ciphertext(s: String, rows: i32) -> String {
if rows ==1 { return s; }
let n = s.len();
let cols = n / rows asusize;
let s = s.as_bytes();
letmut ans = Vec::with_capacity(n);
for d in0..cols {
for i in0..rows asusize {
let j = d + i;
if j < cols {
ans.push(s[i * cols + j]);
}
}
}
while ans.last() == Some(&b' ') {
ans.pop();
}
String::from_utf8(ans).unwrap()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
decodeCiphertext(s: string, rows: number):string {
if (rows===1) returns;
constn=s.length;
constcols= Math.floor(n/rows);
letans='';
for (letd=0; d<cols; d++) {
for (leti=0; i<rows; i++) {
constj=d+i;
if (j<cols) {
ans+=s[i*cols+j];
}
}
}
returnans.replace(/\s+$/, '');
}
}