연습장/백준(BOJ) 문제풀이

[백준 1181] 단어 정렬 with Node.js

Tesseractjh 2021. 4. 9. 20:42

문제 링크

www.acmicpc.net/problem/1181

 

1181번: 단어 정렬

첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.

www.acmicpc.net

풀이

const [n, ...words] = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");
Array.from(new Set(words))
    .sort((a, b) => a > b ? 1 : (a < b ? -1 : 0))
    .sort((a, b) => a.length - b.length)
    .forEach(i => console.log(i));

Array.from(new Set(words))로 중복을 제거한 후 다시 배열로 만든다. 첫 번째 sort로 사전 순서대로 배열하고 두 번째 sort로 길이 순으로 배열하였다.