✨ 숫자 짝궁

나의 풀이
function solution(X, Y) {
let answer = "";
X = X.split('');
Y = Y.split('');
for(let i = 0; i < 10; i++) {
let xCountForI = X.filter(x => Number(x) === i).length;
let yCountForI = Y.filter(y => Number(y) === i).length;
answer += i.toString().repeat(Math.min(xCountForI, yCountForI));
}
if(answer === "") {
return "-1";
}
else if(answer.split('').filter(x => x === "0").length === answer.length) {
return "0";
}
else {
return answer.split('').sort().reverse().join('');
}
}
다른사람의 풀이
function solution(X, Y) {
let result = '';
const numObj = {};
for (const char of X) {
numObj[char] = (numObj[char] || 0) + 1;
}
for (const char of Y) {
if (!numObj[char]) continue;
result += char;
numObj[char]--;
}
if (result === '') return '-1';
if (+result === 0) return '0';
return [...result]
.map((num) => +num)
.sort((a, b) => b - a)
.join('');
}