Skip to content

Commit 1a5cc84

Browse files
authored
[PGS] 17677 [1차] 뉴스 클러스터링 (Lv.2)
1 parent 1d91b59 commit 1a5cc84

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

박예진/0주차/수학/17677.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// 특수문자 버림
2+
function isAlpha(alpha){
3+
if (('a' <= alpha && alpha <= 'z')||
4+
('A' <= alpha && alpha <= 'Z')) return true;
5+
return false;
6+
}
7+
8+
// 2글자씩 묶기
9+
function makeMultiSet(str) {
10+
const arr = [];
11+
for(let i = 0; i < str.length - 1; i++){
12+
if (isAlpha(str[i]) && isAlpha(str[i + 1])) {
13+
arr.push((str[i] + str[i + 1]).toLowerCase());
14+
}
15+
}
16+
return arr;
17+
}
18+
19+
// 배열 -> 빈도수 map
20+
function makeCountMap(arr){
21+
const m = new Map();
22+
for(const a of arr){
23+
m.set(a, (m.get(a) || 0) + 1);
24+
}
25+
return m;
26+
}
27+
28+
function solution(str1, str2) {
29+
var answer = 0;
30+
31+
const arr1 = makeMultiSet(str1);
32+
const arr2 = makeMultiSet(str2);
33+
34+
const map1 = makeCountMap(arr1);
35+
const map2 = makeCountMap(arr2);
36+
37+
let intersection = 0; // 교집합
38+
let union = 0; // 합집합
39+
40+
// 교집합
41+
for (const [key, value] of map1) {
42+
if (map2.has(key)) {
43+
intersection += Math.min(value, map2.get(key));
44+
}
45+
}
46+
47+
// 합집합
48+
const keys = new Set([...map1.keys(), ...map2.keys()]);
49+
for (const key of keys) {
50+
const c1 = map1.get(key) || 0;
51+
const c2 = map2.get(key) || 0;
52+
union += Math.max(c1, c2);
53+
}
54+
55+
// 자카드 유사도
56+
if (union === 0) return 65536;
57+
return Math.floor((intersection / union) * 65536);
58+
59+
return answer;
60+
}

0 commit comments

Comments
 (0)