알파벳 소문자로 이루어진 N개의 단어가 들어오면 아래와 같은 조건에 따라 정렬하는 프로그램을 작성하시오.
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
조건에 따라 정렬하여 단어들을 출력한다.
13
but
i
wont
hesitate
no
more
no
more
it
cannot
wait
im
yours
i
im
it
no
but
more
wait
wont
yours
cannot
hesitate
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const words = input.slice(1);
const uniqueWords = [...new Set(words)];
uniqueWords.sort((a, b) => {
return a.length !== b.length ? a.length - b.length : a.localeCompare(b);
});
console.log(uniqueWords.join("\n"));
Set을 이용해 중복을 제거했다.sort() 메서드를 사용하며, 아래 조건에 따라 정렬했다.localeCompare()를 사용하여 문자열을 비교했다.