function separate(str) {
let result = [];
let regEx = /[^a-z]/
str = str.toLowerCase();
for(let i = 0; i < str.length-1; i++) {
if(regEx.test(str.slice(i,i+2))) continue;
result.push(str.slice(i,i+2));
}
return result;
}
function solution(str1, str2) {
str1 = separate(str1);
str2 = separate(str2);
if(!str1.length && !str2.length) return 65536;
let union = [];
let intersection = [];
for(let i = 0; i<str2.length; i++) {
if(str1.includes(str2[i])) {
intersection.push(...str1.splice(str1.indexOf(str2[i]),1));
}
union.push(str2[i]);
}
union.push(...str1)
return parseInt(intersection.length / union.length * 65536);
}
다중집합에 대해 교집합 합집합을 구하는 방법
let union = [];
let intersection = [];
for(let i = 0; i<arr1.length; i++) {
if(arr2.includes(arr1[i])) {
intersection.push(arr2.splice(arr2.indexOf(arr1[i]),1))
}
union.push(arr1[i]);
}
union.push(...arr2);
splice가 핵심이다.