문제 링크
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로 길이 순으로 배열하였다.
'연습장 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 10815] 숫자 카드 with Python (0) | 2021.04.10 |
---|---|
[백준 7568] 덩치 with Node.js (0) | 2021.04.09 |
[백준 11866] 요세푸스 문제 0 with Python (0) | 2021.04.09 |
[백준 1427] 소트인사이드 with Node.js (0) | 2021.04.08 |
[백준 2609] 최대공약수와 최소공배수 with Node.js (0) | 2021.04.08 |