CodeKata/JS

숫자 짝꿍

explosion149 2024. 12. 19. 09:49
function solution(X = "12321", Y = "42531") {
    const countX = Array(10).fill(0);
    const countY = Array(10).fill(0);

    for (const digit of X) countX[Number(digit)]++;
    for (const digit of Y) countY[Number(digit)]++;

    let result = '';

    for (let i = 9; i >= 0; i--) {
        const commonCount = Math.min(countX[i], countY[i]);
        result += String(i).repeat(commonCount);
    }


    if (result === '') return '-1';
    if (result === "0".repeat(result.length)) return '0';

    return result;
}