Post

[Algorithm] BOJ_20920_영단어_암기는_괴로워

BOJ_20920_영단어_암기는_괴로워 접근방식

[Algorithm] BOJ_20920_영단어_암기는_괴로워

BOJ_20920_영단어_암기는_괴로워

문제 링크

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

카테고리

Hash Map

접근 방식

Hash map에 빈도수를 저장하고 빈도수를 정렬해서 해결하였다.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package ver2.BOJ_20920_영단어_암기는_괴로워;

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        HashMap<String, Integer> map = new HashMap<>();

        int n = sc.nextInt();
        int m = sc.nextInt();

        for (int i = 0; i < n; i++) {
            String st = sc.next();

            if(st.length() < m ) continue;

            map.put(st,map.getOrDefault(st,0) + 1);
        }

        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());

        Collections.sort(list, (a,b) -> {
            if(a.getValue() != b.getValue()){
                return b.getValue() - a.getValue();
            }
            else if(a.getKey().length()!= b.getKey().length()){
                return b.getKey().length() - a.getKey().length();
            }
            return a.getKey().compareTo(b.getKey());
        });

        for(int i = 0 ; i < list.size(); i++){
            System.out.println(list.get(i).getKey());
        }
    }
}

/*
# 카테고리
Hash Map

# 접근 방식
Hash map에 빈도수를 저장하고 빈도수를 정렬해서 해결하였다.

# 문제 링크
https://www.acmicpc.net/problem/20920
 */
This post is licensed under CC BY 4.0 by the author.