Seize your moment! ๐Ÿ‘พ

์•ˆ๋…•ํ•˜์„ธ์š”. Eric์ž…๋‹ˆ๋‹ค. ์ œ ๋ธ”๋กœ๊ทธ์— ๋ฐฉ๋ฌธํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ป ๊ฐœ๋ฐœ๊ณต๋ถ€/Algorithm

[Eirc's ๋ฐฑ์ค€] 1764๋ฒˆ - ๋“ฃ๋ณด์žก - Java ์•Œ๊ณ ๋ฆฌ์ฆ˜

Eric_ko 2023. 1. 15. 10:43

 

์ €๋Š” ํ•ด์‰ฌ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

import java.util.*;

public class N1764 {
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int n = kb.nextInt();
        int m = kb.nextInt();
        String[] a = new String[n];
        String[] b = new String[m];
        for (int i = 0; i < n; i++) {
            a[i] = kb.next();
        }
        for (int i = 0; i < m; i++) {
            b[i] = kb.next();
        }
        for (String x : solution(a, b)) {
            System.out.println(x);
        }
    }
    public static ArrayList<String> solution(String[] a , String [] b) {
        ArrayList<String> answer = new ArrayList<>();
        HashMap<String, Integer> mapA = new HashMap<>();
        //๋“ฃ๋„ ๋ชปํ•œ์‚ฌ๋žŒ ๋„ฃ๊ธฐ
        for (String x : a) {
            mapA.put(x, mapA.getOrDefault(x, 0) + 1);
        }
        //๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ ๊ฐ’ ์ค‘๋ณต์ฒดํฌ
        for (String x : b) {
            mapA.put(x, mapA.getOrDefault(x, 0) -1); // ์ค‘๋ณต๋œ String ์ œ๊ฑฐ
        }
        int cnt = 0;
        for (String key : mapA.keySet()) {
            if (mapA.get(key) == 0) {
                answer.add(key);
                cnt++; // ๊ฐฏ์ˆ˜์„ธ๊ธฐ
            }
        }
        answer.add(String.valueOf(cnt));
        Collections.sort(answer); // ์ •๋ ฌ
        return answer; 
    }
}

Solved.ac ํ”„๋กœํ•„