Skip to content

Commit 4a0899b

Browse files
authored
Create 2075-decode-the-slanted-ciphertext.js
1 parent cee6733 commit 4a0899b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

2075-decode-the-slanted-ciphertext.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string} encodedText
3+
* @param {number} rows
4+
* @return {string}
5+
*/
6+
var decodeCiphertext = function(encodedText, rows) {
7+
let n = encodedText.length;
8+
let cols = ~~(n / rows);
9+
const matrix = Array.from({ length: rows }, () => Array(cols).fill(''))
10+
for (let i = 0; i < rows; i++) {
11+
for (let j = 0; j < cols; j++) {
12+
matrix[i][j] = encodedText[i * cols + j];
13+
}
14+
}
15+
let ans = "";
16+
for (let i = 0; i < cols; i++) {
17+
let t = Math.min(rows, cols - i);
18+
for (let j = 0; j < t; j++) {
19+
ans += matrix[j][i + j];
20+
}
21+
}
22+
let idx = ans.length - 1
23+
for(; idx >= 0; idx--) {
24+
if(ans[idx] === ' ') continue
25+
else break
26+
}
27+
return ans.slice(0, idx + 1);
28+
};
29+

0 commit comments

Comments
 (0)