class Solution:
    def decodeCiphertext(self, encodedText: str, rows: int) -> str:
        n = len(encodedText)
        cols = n // rows
        step = cols + 1
        res = ""
        
        for i in range(cols):
            for j in range(i, n, step):
                res += encodedText[j]
            
        return res.rstrip()
class Solution {
    public String decodeCiphertext(String str, int rows) {

        //first find column size!!
    	int cols=str.length()/rows;
    	StringBuilder res=new StringBuilder(),new_res=new StringBuilder();;
    	for(int i=0;i<cols;i++) {
        
            //iterating diagonally!!
            for(int j=i;j<str.length();j+=cols+1)
    			res.append(str.charAt(j));
    	}
        
        //removing last spaces!!!
        int fg=0;
        for(int i=res.length()-1;i>=0;i--) {
            
            if(fg==0&&res.charAt(i)==' ')
                continue;
            fg=1;
            new_res.append(res.charAt(i));
        }
        return new_res.reverse().toString();
    }
}
Decode the Slanted Ciphertext
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 rows rows.

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 string originalText.

Note: originalText does not have any trailing spaces ' '. The test cases are generated such that there is only one possible originalText.

&nbsp;
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.


&nbsp;
Constraints:


	0 &lt;= encodedText.length &lt;= 106
	encodedText consists of lowercase English letters and ' ' only.
	encodedText is a valid encoding of some originalText that does not have trailing spaces.
	1 &lt;= rows &lt;= 1000
	The testcases are generated such that there is only one possible originalText.


var decodeCiphertext = function(encodedText, rows) {
    const numColumns = encodedText.length / rows;
    const stringBuilder = [];
    let nextCol = 1;
    let row = 0;
    let col = 0;
    let index = 0
    while (index < encodedText.length) {
        stringBuilder.push(encodedText[index]);
        if (row === rows - 1 || col === numColumns - 1) {
            row = 0;
            col = nextCol;
            nextCol++;
        } else {
            row++;
            col++;
        }
        index = calcIndex(row, col, numColumns);
    }
    while (stringBuilder[stringBuilder.length - 1] === ' ') {
        stringBuilder.pop();
    }
    return stringBuilder.join('');
};

function calcIndex(row, col, numColumns) {
    return row * numColumns + col;
}
class Solution {
public:
    string decodeCiphertext(string encodedText, int rows) {
        int n = encodedText.size();

        // Determining the number of columns
        int cols = n / rows;
        vector<vector<char>> mat(rows, vector<char>(cols, ' '));
        int i = 0, j = 0;
        int k = 0;

        string ans = "";

        // Filling the matrix using encodedText
        // Row wise
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                mat[i][j] = encodedText[k++];
            }
        }

        // Only the upper triangular part of the matrix will
        // contain characters of the originalText
        // so, this loop traverses that area
        for(int k = 0; k < n - (rows * (rows - 1)) / 2; k++) {
            // i, j are the two pointers for tracking rows and columns
            ans.push_back(mat[i++][j++]);

            // If any boundary is hit, then column pointer is subtracted
            // by row_pointer - 1
            // and row pointer is reset to 0
            if(i == rows || j == cols) {
                j -= (i - 1);
                i = 0;
            }
        }

        // Removing all trailing spaces
        while(ans.back() == ' ')
            ans.pop_back();

        return ans;
    }
};