
- Code
import java.io.*;
import java.util.*;
public class BOJ20920 {
public static void main(String[] args) throws IOException {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int wordCnt = Integer.parseInt(st.nextToken());
int standardLength = Integer.parseInt(st.nextToken());
// 입력 단어가 길이 이상인 겨우 총 갯수와 같이 저장
Map<String, Integer> wordAndCnt = new HashMap<>();
for (int i = 0; i < wordCnt; i++) {
String word = br.readLine();
if (word.length() >= standardLength) {
wordAndCnt.put(
word,
wordAndCnt.getOrDefault(word, 0) + 1
);
}
}
// 정렬
List<String> wordSort = new ArrayList<>(wordAndCnt.keySet());
wordSort.sort((o1, o2) -> {
// 자주 나오는 단어 순
if (!wordAndCnt.get(o2).equals(wordAndCnt.get(o1))) {
return wordAndCnt.get(o2) - wordAndCnt.get(o1);
}
// 단어의 길이가 긴 순
if (o2.length() != o1.length()) {
return o2.length() - o1.length();
}
// 알파벳 사전 순
return o1.compareTo(o2);
});
// 출력
StringBuilder answer = new StringBuilder();
for (String word : wordSort) {
answer.append(word).append("\n");
}
System.out.println(answer);
}
}'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [백준/Java] 10431번 줄세우기 (0) | 2026.02.16 |
|---|---|
| [백준/Java] 6693번 Complicated Route (0) | 2026.02.16 |
| [백준/Java] 16967번 배열 복원하기 (0) | 2026.02.15 |
| [백준/Java] 15803번 PLAYERJINAH’S BOTTLEGROUNDS (0) | 2026.02.14 |
| [백준/Java] 4201번 Snakes and Ladders (0) | 2026.02.14 |