LeetCode 2075: Decode the Slanted Ciphertext Solution
Master LeetCode problem 2075 (Decode the Slanted Ciphertext), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2075. Decode the Slanted Ciphertext
Problem Explanation
Explanation
To decode the slanted ciphertext, we will simulate the encoding process in reverse. We will iterate through the encoded text and fill the characters back into a matrix based on the given number of rows. By observing the pattern of how the characters were filled during encoding, we can reconstruct the original text.
Time complexity: O(n), where n is the length of the encoded text. Space complexity: O(n), where n is the length of the encoded text.
Solution Code
class Solution {
public String decodeSlantedCiphertext(String encodedText, int rows) {
if (rows == 1) {
return encodedText;
}
int cols = (encodedText.length() + rows - 1) / rows;
char[][] matrix = new char[rows][cols];
int index = 0;
for (int c = 0; c < cols; c++) {
for (int r = 0; r < rows; r++) {
if (c % 2 == 0 || r == rows - 1) {
matrix[r][c] = encodedText.charAt(index++);
}
}
}
StringBuilder originalText = new StringBuilder();
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (matrix[r][c] != 0) {
originalText.append(matrix[r][c]);
}
}
}
return originalText.toString();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2075 (Decode the Slanted Ciphertext)?
This page provides optimized solutions for LeetCode problem 2075 (Decode the Slanted Ciphertext) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2075 (Decode the Slanted Ciphertext)?
The time complexity for LeetCode 2075 (Decode the Slanted Ciphertext) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2075 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2075 in Java, C++, or Python.