Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 989aa67

Browse files
add 2075
1 parent dfa38c9 commit 989aa67

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2075|[Decode the Slanted Ciphertext](https://leetcode.com/problems/decode-the-slanted-ciphertext/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2075.java) ||Medium||
1112
|2074|[Reverse Nodes in Even Length Groups](https://leetcode.com/problems/reverse-nodes-in-even-length-groups/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2074.java) ||Medium||
1213
|2073|[Time Needed to Buy Tickets](https://leetcode.com/problems/time-needed-to-buy-tickets/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2073.java) ||Easy||
1314
|2070|[Most Beautiful Item for Each Query](https://leetcode.com/problems/most-beautiful-item-for-each-query/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2070.java) ||Medium||
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2075 {
4+
public static class Solution1 {
5+
public String decodeCiphertext(String encodedText, int rows) {
6+
if (rows == 1) {
7+
return encodedText;
8+
}
9+
int total = encodedText.length();
10+
int cols = total / rows;
11+
char[][] grid = new char[rows][cols];
12+
int index = 0;
13+
for (int i = 0; i < rows; i++) {
14+
for (int j = 0; j < cols; j++) {
15+
grid[i][j] = encodedText.charAt(index++);
16+
}
17+
}
18+
StringBuilder sb = new StringBuilder();
19+
int colIndex = 0;
20+
while (colIndex < cols) {
21+
for (int j = colIndex, i = 0; j < cols && i < rows; j++, i++) {
22+
sb.append(grid[i][j]);
23+
}
24+
colIndex++;
25+
}
26+
int i = sb.length() - 1;
27+
while (i >= 0 && sb.charAt(i) == ' ') {
28+
i--;
29+
}
30+
return sb.substring(0, i + 1);
31+
}
32+
}
33+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2075;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2075Test {
10+
private static _2075.Solution1 solution1;
11+
private static String encodedText;
12+
private static int rows;
13+
private static String expected;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _2075.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
encodedText = "ch ie pr";
23+
rows = 3;
24+
expected = "cipher";
25+
assertEquals(expected, solution1.decodeCiphertext(encodedText, rows));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
encodedText = "iveo eed l te olc";
31+
rows = 4;
32+
expected = "i love leetcode";
33+
assertEquals(expected, solution1.decodeCiphertext(encodedText, rows));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
encodedText = "coding";
39+
rows = 1;
40+
expected = "coding";
41+
assertEquals(expected, solution1.decodeCiphertext(encodedText, rows));
42+
}
43+
44+
@Test
45+
public void test4() {
46+
encodedText = " b ac";
47+
rows = 2;
48+
expected = " abc";
49+
assertEquals(expected, solution1.decodeCiphertext(encodedText, rows));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)