https://school.programmers.co.kr/learn/courses/30/lessons/72411#
function getCombinations(arr, selectNum) {
const results = [];
if (selectNum === 1) {
return arr.map((value) => [value]);
}
arr.forEach((value, index, origin) => {
const rest = origin.slice(index + 1);
const combinations = getCombinations(rest, selectNum - 1);
const attached = combinations.map((combination) => [value, ...combination]);
results.push(...attached);
});
return results;
}
function solution(orders, course) {
var answer = [];
const flat = orders.join('')
let set = new Set(flat)
set = [...set].sort() // 오름차순 반환 위해서 sort
for (const ele of course) {
if (set.length < ele) continue
const cbList = getCombinations([...set], ele)
// console.log(cbList)
let map = new Map()
for (const cb of cbList) {
let cnt = 0
for (let order of orders) {
order = order.split('')
// some) 조합의 요소를 돌면서 콜백이 true를 반환하면, true할당 & 즉시 종료됨. 다 돌때까지 true 안나오면 false 할당됨.
let include = !cb.some(ele => !order.includes(ele));
if (include) cnt ++
}
if (cnt>=2) map.set(cb, cnt)
}
if (map.size===0) continue
map = [...map].sort((a,b)=>b[1]-a[1])
// console.log(map)
let maxV = map[0][1]
map.forEach(ele=>{
if (ele[1]===maxV) answer.push(ele[0].join(''))
})
}
return answer.sort()
}
function getCombinations(arr, selectNum) {
const results = [];
if (selectNum === 1) {
return arr.map((value) => [value]);
}
arr.forEach((value, index, origin) => {
const rest = origin.slice(index + 1);
const combinations = getCombinations(rest, selectNum - 1);
const attached = combinations.map((combination) => [value, ...combination]);
results.push(...attached);
});
return results;
}
function solution(orders, course) {
var answer = [];
for (const selectNum of course) {
let map = new Map()
for (let order of orders) {
if (order.length < selectNum) continue
order = order.split('').sort()
const cbList = getCombinations(order, selectNum)
for (let cb of cbList) {
cb = cb.join('')
map.set(cb, (map.get(cb)||0)+1)
}
}
if (map.size===0) continue
map = [...map].sort((a,b)=>b[1]-a[1])
// console.log(map)
let maxV = map[0][1]
if (maxV<2) continue
map.forEach(ele=>{
if (ele[1]===maxV) answer.push(ele[0])
})
}
return answer.sort()
}