204 ms Beats 100.00% TypeScript
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nStep-by-Step Explanation:\nCheck for a single row:\n\nIf there is only 1 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nStep-by-Step Explanation:\nCheck for a single row:\n\nIf there is only 1 row (rows === 1), the encoded text doesn\'t need any transformation, so it is returned as is.\nCalculate columns:\n\nThe number of columns (cols) is calculated based on the length of encodedText divided by the number of rows, rounded up (Math.ceil). This ensures that the matrix is large enough to fit all characters.\nMatrix initialization:\n\nA matrix of strings is initialized to hold the encoded characters, row by row. The matrix will have rows number of rows and cols number of columns.\nPopulating the matrix:\n\nA nested loop is used to fill the matrix with characters from encodedText. For each row, it collects characters from the encoded text and adds them to the row. If there are fewer characters than the total cells in the matrix, it fills the remaining cells with spaces (\' \').\nDecoding the ciphertext:\n\nThe matrix is read column by column, top to bottom. Each column\'s characters are appended to the result string. The inner loop ensures that characters are picked from each row in the current column (matrix[row][temp]).\nTrimming trailing spaces:\n\nThe trimRight() function removes any extra spaces from the decoded string at the end, giving the final result.\nOptional Debugging:\nThe console.log(matrix) is included for debugging purposes to see how the matrix looks after it\'s populated.\n\n# Complexity\n**Time Complexity: O(n)\nSpace Complexity: O(n)**\n\n# Code\n```typescript []\nfunction decodeCiphertext(encodedText: string, rows: number): string {\n    // Check if there\'s only one row, in which case no decoding is necessary. Return the encoded text as is.\n    if (rows === 1) return encodedText;\n\n    // Calculate the number of columns needed to fit the text in a matrix of \'rows\' rows.\n    // The number of columns is the ceiling of the total length of encodedText divided by the number of rows.\n    let cols = Math.ceil(encodedText.length / rows)\n\n    // Initialize an empty matrix, which will store the characters row by row.\n    const matrix: string[][] = [];\n\n    // Initialize an empty string to store the final decoded result.\n    let result = "";\n\n    // Variable to keep track of the index as we go through the encoded text.\n    let index = 0;\n\n    // Loop through each row to populate the matrix.\n    for (let i = 0; i < rows; i++) {\n        // Initialize an empty array for the current row.\n        const row: string[] = [];\n\n        // Loop through each column in the current row.\n        for (let j = 0; j < cols; j++) {\n            // Push the character at the current index into the row.\n            // If index exceeds the length of encodedText, push a space (\' \') instead.\n            row.push(encodedText.charAt(index) || \' \');\n\n            // Increment the index to point to the next character in encodedText.\n            index++;\n        }\n\n        // Push the populated row into the matrix.\n        matrix.push(row);\n    }\n\n    // Optionally log the matrix for debugging purposes.\n    console.log(matrix);\n\n    // Now, loop through each column of the matrix and read characters top-to-bottom, column by column.\n    // This forms the decoded message.\n    for (let col = 0; col < cols; col++) {\n        // Initialize a temporary variable `temp` that keeps track of the column index.\n        let temp = col;\n\n        // Loop through each row to extract characters in the current column.\n        // Stop if we reach the end of the column.\n        for (let row = 0; row < rows && temp < cols; row++) {\n\n            // Add the character from the current position in the matrix to the result string.\n            // The commented-out check (`matrix[row][temp] !== \' \'`) can be used to skip spaces if desired.\n            result += matrix[row][temp];\n\n            // Increment the column index (`temp`) for the next row.\n            temp++;\n        }\n    }\n\n    // Return the result string with trailing spaces removed.\n    return result.trimRight();\n};\n\n```
['String', 'Simulation', 'TypeScript']