๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ฝ”๋”ฉํ…Œ์ŠคํŠธ/TIL

[99ํด๋Ÿฝ] 21์ผ์ฐจ ๋ฌธ์ œ: ํŒŒ์ผ ์ •๋ฆฌ

by moon101 2025. 2. 17.

 

 

๋ฐฑ์ค€ ํŒŒ์ผ ์ •๋ฆฌ ๋ฌธ์ œ. ํ™•์žฅ์ž ๋ช… ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ๋ผ treemap์„ ์‚ฌ์šฉํ•ด์„œ key๋กœ ์ •๋ ฌ.

 

import java.io.*;
import java.util.*;

public class Main {
    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()); // ํŒŒ์ผ ๊ฐœ์ˆ˜

        // ํŒŒ์ผ ํ™•์žฅ์ž ๋ณ„๋กœ ์นด์šดํŠธ
        Map<String, Integer> extensionToFreq = new TreeMap<>();

        // ํŒŒ์ผ ์ €์žฅ
        for (int i = 0; i < N; i++) {
            String s = br.readLine();
            String[] sArr = s.split("\\.");
            
            extensionToFreq.put(sArr[sArr.length -1], extensionToFreq.getOrDefault(sArr[sArr.length -1], 0) + 1);
            
        }

        // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Integer> e : extensionToFreq.entrySet()) {
            sb.append(e.getKey()).append(" ").append(e.getValue()).append("\n");
        }
        System.out.print(sb);
    }
}
 

๋Œ“๊ธ€