📚 문제
https://programmers.co.kr/learn/courses/30/lessons/72411
💡 접근
⌨️ 코드
function solution(orders, course) {
let listObj = {};
let answer = [];
function combination(arr, n) {
let result = [];
if (n === 1) return arr.map((v) => [v]);
arr.forEach((fixed, idx, origin) => {
let rest = origin.slice(idx + 1);
let combinationArr = combination(rest, n - 1);
let attached = combinationArr.map((c) => [fixed, ...c]);
result.push(...attached);
});
return result;
}
orders.map((o) => {
const orderArr = o.split("").sort();
for (let i = 0; i < course.length; i++) {
const orderCombis = combination(orderArr, course[i]);
orderCombis.map(orderCombi => {
const string = orderCombi.join("");
if (listObj[string]) listObj[string] += 1;
else listObj[string] = 1;
});
}
});
const listArr = Object.entries(listObj);
course.map(c => {
const candidates = listArr.filter(l => l[0].length == c && l[1] > 1);
if (candidates.length > 0){
let max = Math.max(... candidates.map(v => v[1]));
candidates.forEach(v => {
if (v[1] == max) answer.push(v[0]);
})
}
})
return answer.sort();
}
📝 리뷰