File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments