문제풀이/구현

[Javascript/자바스크립트 ] (프로그래머스) [1차] 뉴스 클러스터링

딜레이레이 2024. 12. 21. 16:13

https://school.programmers.co.kr/learn/courses/30/lessons/17677?language=javascript

코드

function isAlpha(str) {
  return /^[A-Za-z]+$/.test(str);
}

function getFragment(str) {
  const result = [];
  for (let i = 0; i < str.length - 1; i++) {
    const fragment = str.slice(i, i + 2).toLowerCase();
    if (!isAlpha(fragment)) continue;
    result.push(fragment);
  }
  return result;
}

function solution(str1, str2) {
  const str1Fragments = getFragment(str1);
  const str2Fragments = getFragment(str2);
  const set = new Set([...str1Fragments, ...str2Fragments]);

  let intersection = 0;
  let union = 0;

  set.forEach((frag) => {
    const cnt1 = str1Fragments.filter((value) => value === frag).length;
    const cnt2 = str2Fragments.filter((value) => value === frag).length;
    union += Math.max(cnt1, cnt2);
    intersection += Math.min(cnt1, cnt2);
  });

  return union === 0 ? 65536 : Math.floor((intersection / union) * 65536);
}