HIT해

[JS 백준] 10798.세로읽기 본문

Vue/JavaScript 알고리즘

[JS 백준] 10798.세로읽기

힛해 2024. 1. 5. 14:33
728x90
const words = require('fs')
.readFileSync(__dirname + "/example.txt")
.toString().trim()
.split("\n");

const maxLength = Math.max(...words.map(i => i.trim().length));

console.log("최대길이는" + maxLength);

let vertical = "";
for (let i=0; i<maxLength; i++) {
    for (let j=0; j<words.length; j++) {
        vertical += words[j][i] || "";
    }
}
console.log(vertical);

 

Math.max 함수 내에서 trim을 다시 해서 최대길이를 확인하지않으면

공백을 포함해서 1이 증가한 문자열 길이를 반환한다.

'Vue > JavaScript 알고리즘' 카테고리의 다른 글

[JS 백준] 28279.덱 2  (0) 2024.01.07
[JS 백준] 9012.괄호  (0) 2024.01.07
[JS 백준] 1929.소수 구하기  (0) 2024.01.06
[JS 백준] 1978.소수찾기  (0) 2024.01.05
[JS 백준] 2563.색종이  (0) 2024.01.04