Skip to content

Commit e0e46b7

Browse files
chore: add LeetCode daily solution
1 parent 1d5f8f9 commit e0e46b7

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Decode the Slanted Ciphertext (Medium)
2+
3+
**Problem ID:** 2075
4+
**Date:** 2026-04-04
5+
**Link:** https://leetcode.com/problems/decode-the-slanted-ciphertext/
6+
7+
## Approach
8+
9+
To solve the problem of decoding the slanted ciphertext, we need to reverse the encoding process described. The approach can be broken down into the following steps:
10+
11+
1. **Understanding the Matrix Structure**: The encoded text is derived from a matrix filled in a specific order. The matrix has a fixed number of rows, and the characters from `originalText` are filled diagonally. The first step is to determine the number of columns in the matrix. This can be calculated as:
12+
- `columns = (length of encodedText + rows - 1) // rows`
13+
This formula ensures that we have enough columns to accommodate the entire `encodedText` when filled in the specified manner.
14+
15+
2. **Reconstructing the Matrix**: Using the calculated number of columns, we can reconstruct the matrix from the `encodedText`. The matrix will be filled row by row, where each row will contain `columns` characters from the `encodedText`.
16+
17+
3. **Filling the Matrix**: We fill the matrix by iterating over the `encodedText` and placing characters in their respective positions according to the diagonal filling order. This involves:
18+
- For each column, starting from the top row and moving downwards, we fill the matrix diagonally until we reach the bottom row or the end of the string.
19+
20+
4. **Extracting the Original Text**: Once the matrix is filled, we can extract the `originalText` by reading the matrix in a row-wise manner. This means concatenating characters from each row sequentially to form the final decoded string.
21+
22+
5. **Handling Edge Cases**: We need to consider cases where `encodedText` is empty or when there is only one row, in which case the output would be the same as the input.
23+
24+
**Data Structures**: A 2D list (matrix) is used to store the characters while reconstructing the matrix. This allows for easy access and modification of individual elements.
25+
26+
**Complexity**: The time complexity of this approach is O(n), where n is the length of `encodedText`, as we are iterating through the string a constant number of times. The space complexity is also O(n) due to the storage of the matrix.
27+
28+
By following this structured approach, we can efficiently decode the slanted ciphertext back to the original text.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public String decodeCiphertext(String encodedText, int rows) {
3+
int n = encodedText.length();
4+
if (n == 0 || rows == 0) return "";
5+
6+
int cols = (n + rows - 1) / rows; // Calculate the number of columns
7+
char[][] matrix = new char[rows][cols];
8+
9+
// Fill the matrix with characters from encodedText
10+
for (int i = 0; i < n; i++) {
11+
int r = i % rows;
12+
int c = i / rows;
13+
if (c < cols) {
14+
matrix[r][c] = encodedText.charAt(i);
15+
}
16+
}
17+
18+
StringBuilder originalText = new StringBuilder();
19+
20+
// Read the matrix in the slanted order
21+
for (int diag = 0; diag < cols; diag++) {
22+
for (int r = 0; r < rows; r++) {
23+
int c = diag - r;
24+
if (c >= 0 && c < cols && matrix[r][c] != '\u0000') {
25+
originalText.append(matrix[r][c]);
26+
}
27+
}
28+
}
29+
30+
return originalText.toString().trim(); // Remove trailing spaces
31+
}
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var decodeCiphertext = function(encodedText, rows) {
2+
const n = encodedText.length;
3+
const cols = Math.ceil(n / rows);
4+
let originalText = '';
5+
6+
for (let i = 0; i < cols; i++) {
7+
for (let j = 0; j < rows; j++) {
8+
const index = j + i + j * (cols - 1);
9+
if (index < n) {
10+
originalText += encodedText[index];
11+
}
12+
}
13+
}
14+
15+
return originalText.replace(/\s+$/, '');
16+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def decodeCiphertext(self, encodedText: str, rows: int) -> str:
3+
if rows == 0 or not encodedText:
4+
return ""
5+
6+
n = len(encodedText)
7+
cols = (n + rows - 1) // rows
8+
matrix = [[' ' for _ in range(cols)] for _ in range(rows)]
9+
10+
idx = 0
11+
for r in range(rows):
12+
for c in range(cols):
13+
if idx < n:
14+
matrix[r][c] = encodedText[idx]
15+
idx += 1
16+
17+
originalText = []
18+
for diag in range(cols):
19+
r, c = 0, diag
20+
while r < rows and c >= 0:
21+
if matrix[r][c] != ' ':
22+
originalText.append(matrix[r][c])
23+
r += 1
24+
c -= 1
25+
26+
return ''.join(originalText).rstrip()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
413413
- 2026-04-01 — [Robot Collisions](https://leetcode.com/problems/robot-collisions/) (Hard) → `Hard/2026-04-01-2751-Robot-Collisions`
414414
- 2026-04-02 — [Maximum Amount of Money Robot Can Earn](https://leetcode.com/problems/maximum-amount-of-money-robot-can-earn/) (Medium) → `Medium/2026-04-02-3418-Maximum-Amount-of-Money-Robot-Can-Earn`
415415
- 2026-04-03 — [Maximum Walls Destroyed by Robots](https://leetcode.com/problems/maximum-walls-destroyed-by-robots/) (Hard) → `Hard/2026-04-03-3661-Maximum-Walls-Destroyed-by-Robots`
416+
- 2026-04-04 — [Decode the Slanted Ciphertext](https://leetcode.com/problems/decode-the-slanted-ciphertext/) (Medium) → `Medium/2026-04-04-2075-Decode-the-Slanted-Ciphertext`

0 commit comments

Comments
 (0)