์ ๋ ํด์ฌ๋ฅผ ์ด์ฉํด์ ํ์ด๋ณด์์ต๋๋ค.
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;
}
}
'๐ป ๊ฐ๋ฐ๊ณต๋ถ > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Eric's ๋ฐฑ์ค] 10809๋ฒ - ์ํ๋ฒณ์ฐพ๊ธฐ - Java (0) | 2023.01.26 |
---|---|
[Eric's ๋ฐฑ์ค] 3273๋ฒ - ๋ ์์ ํฉ - Java (0) | 2023.01.18 |
[Eric's ๋ฐฑ์ค] 1712๋ฒ - ์์ต๋ถ๊ธฐ์ - Java (0) | 2023.01.17 |
[Eric's ๋ฐฑ์ค] 1806๋ฒ - ๋ถ๋ถํฉ - Java (0) | 2023.01.16 |
[Eric's ๋ฐฑ์ค] 27160๋ฒ - ํ ๋ฆฌ๊ฐ๋ฆฌ - Java - 2023๋ณด๋๊ฒ์์ปต (0) | 2023.01.15 |