https://school.programmers.co.kr/learn/courses/30/lessons/17677
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
Set
retainAll : 교집합
addAll : 합집합
aa, aa, .. 이런 중복을 허용해야 하므로 개수를 세야 했다
import java.util.*;
class Solution {
public int solution(String str1, String str2) {
int answer = 0;
Map<String,Integer> setOne = createSet(str1);
Map<String, Integer> setTwo = createSet(str2);
Set<String> added = new HashSet<>();
added.addAll(setOne.keySet());
added.addAll(setTwo.keySet());
int x = 0;
for(String a : added) {
x += Math.max(setOne.getOrDefault(a, 0),setTwo.getOrDefault(a, 0));
}
Set<String> oneKeys = new HashSet<>(setOne.keySet());
oneKeys.retainAll(setTwo.keySet());
int y = 0;
for(String o : oneKeys) {
y += Math.min(setOne.getOrDefault(o, 0), setTwo.getOrDefault(o, 0));
}
// addAll : 합집합, retainAll : 교집합
if (x == 0) {
answer = 65536;
} else {
answer = 65536 * y / x;
}
return answer;
}
static boolean isAlphabet(char a) {
return a >= 'a'&& a <= 'z' || a >= 'A' && a <= 'Z';
}
// 영문자 2글자씩
static Map<String, Integer> createSet(String str){
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < str.length() - 1; i++) {
char a = str.charAt(i);
char b = str.charAt(i+1);
if (isAlphabet(a) && isAlphabet(b)) {
String value = ""+a+b;
map.put(value.toLowerCase(), map.getOrDefault(value.toLowerCase(), 0) + 1);
}
}
return map;
}
}
[프로그래머스] 프로세스 (0) | 2025.02.03 |
---|---|
[프로그래머스] k진수에서 소수 개수 구하기 (0) | 2025.02.01 |
[프로그래머스] 전화번호 목록 (0) | 2025.01.31 |
[프로그래머스] 광물 캐기 (0) | 2025.01.31 |
[프로그래머스] 피로도 (0) | 2025.01.31 |