Skip to content

Commit 0888d94

Browse files
authored
Added task 2075.
1 parent 8f5956b commit 0888d94

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g2001_2100.s2075_decode_the_slanted_ciphertext;
2+
3+
// #Medium #String #Simulation #2022_05_29_Time_48_ms_(68.23%)_Space_77.9_MB_(64.71%)
4+
5+
public class Solution {
6+
public String decodeCiphertext(String encodedText, int rows) {
7+
if (rows == 1) {
8+
return encodedText;
9+
}
10+
int total = encodedText.length();
11+
int cols = total / rows;
12+
char[][] grid = new char[rows][cols];
13+
int index = 0;
14+
for (int i = 0; i < rows; i++) {
15+
for (int j = 0; j < cols; j++) {
16+
grid[i][j] = encodedText.charAt(index++);
17+
}
18+
}
19+
StringBuilder sb = new StringBuilder();
20+
int colIndex = 0;
21+
while (colIndex < cols) {
22+
for (int j = colIndex, i = 0; j < cols && i < rows; j++, i++) {
23+
sb.append(grid[i][j]);
24+
}
25+
colIndex++;
26+
}
27+
int i = sb.length() - 1;
28+
while (i >= 0 && sb.charAt(i) == ' ') {
29+
i--;
30+
}
31+
return sb.substring(0, i + 1);
32+
}
33+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2075\. Decode the Slanted Ciphertext
2+
3+
Medium
4+
5+
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`.
6+
7+
`originalText` is placed first in a top-left to bottom-right manner.
8+
9+
![](https://assets.leetcode.com/uploads/2021/11/07/exa11.png)
10+
11+
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`.
12+
13+
`encodedText` is then formed by appending all characters of the matrix in a row-wise fashion.
14+
15+
![](https://assets.leetcode.com/uploads/2021/11/07/exa12.png)
16+
17+
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.
18+
19+
For example, if `originalText = "cipher"` and `rows = 3`, then we encode it in the following manner:
20+
21+
![](https://assets.leetcode.com/uploads/2021/10/25/desc2.png)
22+
23+
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"`.
24+
25+
Given the encoded string `encodedText` and number of rows `rows`, return _the original string_ `originalText`.
26+
27+
**Note:** `originalText` **does not** have any trailing spaces `' '`. The test cases are generated such that there is only one possible `originalText`.
28+
29+
**Example 1:**
30+
31+
**Input:** encodedText = "ch ie pr", rows = 3
32+
33+
**Output:** "cipher"
34+
35+
**Explanation:** This is the same example described in the problem description.
36+
37+
**Example 2:**
38+
39+
![](https://assets.leetcode.com/uploads/2021/10/26/exam1.png)
40+
41+
**Input:** encodedText = "iveo eed l te olc", rows = 4
42+
43+
**Output:** "i love leetcode"
44+
45+
**Explanation:** The figure above denotes the matrix that was used to encode originalText.
46+
47+
The blue arrows show how we can find originalText from encodedText.
48+
49+
**Example 3:**
50+
51+
![](https://assets.leetcode.com/uploads/2021/10/26/eg2.png)
52+
53+
**Input:** encodedText = "coding", rows = 1
54+
55+
**Output:** "coding"
56+
57+
**Explanation:** Since there is only 1 row, both originalText and encodedText are the same.
58+
59+
**Constraints:**
60+
61+
* <code>0 <= encodedText.length <= 10<sup>6</sup></code>
62+
* `encodedText` consists of lowercase English letters and `' '` only.
63+
* `encodedText` is a valid encoding of some `originalText` that **does not** have trailing spaces.
64+
* `1 <= rows <= 1000`
65+
* The testcases are generated such that there is **only one** possible `originalText`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2001_2100.s2075_decode_the_slanted_ciphertext;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void decodeCiphertext() {
11+
assertThat(new Solution().decodeCiphertext("ch ie pr", 3), equalTo("cipher"));
12+
}
13+
14+
@Test
15+
void decodeCiphertext2() {
16+
assertThat(
17+
new Solution().decodeCiphertext("iveo eed l te olc", 4),
18+
equalTo("i love leetcode"));
19+
}
20+
21+
@Test
22+
void decodeCiphertext3() {
23+
assertThat(new Solution().decodeCiphertext("coding", 1), equalTo("coding"));
24+
}
25+
}

0 commit comments

Comments
 (0)