240304 뉴스 클러스터링

Jongleee·2024년 3월 4일
0

TIL

목록 보기
511/576
public int solution(String str1, String str2) {
	Map<String, Integer> map1 = new HashMap<>();
	Map<String, Integer> map2 = new HashMap<>();

	cluster(str1.toLowerCase(), map1);
	cluster(str2.toLowerCase(), map2);

	int union = 0;
	int intersection = 0;

	Set<String> set = new HashSet<>(map1.keySet());
	set.addAll(map2.keySet());

	for (String key : set) {
		int a = map1.getOrDefault(key, 0);
		int b = map2.getOrDefault(key, 0);
		union += Math.max(a, b);
		intersection += Math.min(a, b);
	}

	if (union == 0) {
		return 65536;
	}

	return (int) (((double) intersection / (double) union) * 65536);
}

private void cluster(String str, Map<String, Integer> map) {
	for (int i = 0; i <= str.length() - 2; i++) {
		String s = str.substring(i, i + 2);
		if (Character.isLetter(s.charAt(0)) && Character.isLetter(s.charAt(1))) {
			map.put(s, map.getOrDefault(s, 0) + 1);
		}
	}
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/17677

0개의 댓글