Decode the Slanted Ciphertext LeetCode solution in Java

1
class Solution {
2
public String decodeCiphertext(String str, int rows) {
3

4
// first find column size!!
5
int cols = str.length() / rows;
6
StringBuilder res = new StringBuilder(), new_res = new StringBuilder();
7
;
8
for (int i = 0; i < cols; i++) {
9

10
// iterating diagonally!!
11
for (int j = i; j < str.length(); j += cols + 1) res.append(str.charAt(j));
12
}
13

14
// removing last spaces!!!
15
int fg = 0;
16
for (int i = res.length() - 1; i >= 0; i--) {
17

18
if (fg == 0 && res.charAt(i) == ' ') continue;
19
fg = 1;
20
new_res.append(res.charAt(i));
21
}
22
return new_res.reverse().toString();
23
}
24
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0