forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2075.java
33 lines (32 loc) · 1.05 KB
/
_2075.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.fishercoder.solutions;
public class _2075 {
public static class Solution1 {
public String decodeCiphertext(String encodedText, int rows) {
if (rows == 1) {
return encodedText;
}
int total = encodedText.length();
int cols = total / rows;
char[][] grid = new char[rows][cols];
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j] = encodedText.charAt(index++);
}
}
StringBuilder sb = new StringBuilder();
int colIndex = 0;
while (colIndex < cols) {
for (int j = colIndex, i = 0; j < cols && i < rows; j++, i++) {
sb.append(grid[i][j]);
}
colIndex++;
}
int i = sb.length() - 1;
while (i >= 0 && sb.charAt(i) == ' ') {
i--;
}
return sb.substring(0, i + 1);
}
}
}