Back to April Back to Year 2026
MEDIUM 04 Apr 2026 View on LeetCode

2075. Decode the Slanted Ciphertext

</> Solution

/**
 * Problem: LeetCode 2075 - Decode the Slanted Ciphertext
 * Difficulty: Medium
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public String decodeCiphertext(String encodedText, int rows) {
        if (rows == 1 || encodedText == null || encodedText.isEmpty()) {
            return encodedText;
        }

        int n = encodedText.length();
        int cols = n / rows;
        StringBuilder sb = new StringBuilder(n);

        for (int c = 0; c < cols; c++) {
            for (int r = 0; r < rows && c + r < cols; r++) {
                sb.append(encodedText.charAt(r * cols + (c + r)));
            }
        }

        int len = sb.length();
        while (len > 0 && sb.charAt(len - 1) == ' ') {
            len--;
        }
        sb.setLength(len);

        return sb.toString();
    }
}

TIME COMPLEXITY

O(N)

SPACE COMPLEXITY

O(N)

TOPICS

Matrix Simulation String