Skip to content

Commit ce25b57

Browse files
authored
Create 1324-print-words-vertically.js
1 parent 388dbb5 commit ce25b57

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

1324-print-words-vertically.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @return {string[]}
4+
*/
5+
const printVertically = function(s) {
6+
const arr = s.split(' ').filter(e => e !== '')
7+
const m = arr.length
8+
let n = 0
9+
for(const e of arr) {
10+
n = Math.max(n, e.length)
11+
}
12+
13+
const mat = Array.from({ length: m }, () => Array(n).fill(' '))
14+
for(let i = 0; i < arr.length; i++) {
15+
const cur = mat[i]
16+
for(let j = 0; j < arr[i].length; j++) {
17+
mat[i][j] = arr[i][j]
18+
}
19+
}
20+
const res = []
21+
for(let j = 0; j < n; j++) {
22+
const col = []
23+
for(let i = 0; i < m; i++) {
24+
col.push(mat[i][j])
25+
}
26+
res.push(col.join('').trimEnd())
27+
}
28+
29+
return res
30+
};

0 commit comments

Comments
 (0)