📌 고정 게시글

📢 공지합니다

이 게시글은 메인 페이지에 항상 고정되어 표시됩니다.

최코딩의 개발

[백준] 영단어 암기는 괴로워 20920번 (🥈 실버3) 본문

코딩테스트/백준

[백준] 영단어 암기는 괴로워 20920번 (🥈 실버3)

seung_ho_choi.s 2024. 9. 15. 22:38
728x90

사이트

https://www.acmicpc.net/problem/20920

 

문제

전체코드

package datastructure;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class DataStructure20920 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        HashMap<String, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String word2 = br.readLine();
            if (word2.length() >= m) {
                // 중복 체크 최적화
                hashMap.put(word2, hashMap.getOrDefault(word2, 0) + 1);
            }
        }

        // 1. HashMap을 List로 변환
        List<Map.Entry<String, Integer>> list = new ArrayList<>(hashMap.entrySet());

        list.sort((e1,e2)-> {
            int count1 = e1.getValue();
            int count2= e2.getValue();
            String word1 = e1.getKey();
            String word2 = e2.getKey();

            if(count1 != count2){
                return count2 - count1;
            }
            if(word2.length() != word1.length()){
                return word2.length()- word1.length();
            }

            return word1.compareTo(word2);
        });

        StringBuilder sb = new StringBuilder();

        for (Map.Entry<String, Integer> entry : list) {
            sb.append(entry.getKey()).append("\n");
        }
        System.out.print(sb);
    }
}

결과

느낀점

서류 붙어서 지금 코테준비하느라 시간이없다 ㅏㅏㅏㅏㅏㅏ 

map을 오랜만에 풀어본다

728x90