LeetCode 2283번 Check if Number Has Equal Digit Count and Digit Value JavaScript

찌니월드·2025년 4월 9일

문제

You are given a 0-indexed string num of length n consisting of digits.

Return true if for every index i in the range 0 <= i < n, the digit i occurs num[i] times in num, otherwise return false.

문제 출처

2283. Check if Number Has Equal Digit Count and Digit Value

나의 풀이

/**
 * @param {string} num
 * @return {boolean}
 */
var digitCount = function(num) {
    const arr = num.split("").map(Number);
    const sH = new Map();

    for (let num of arr) {
        if (sH.has(num)) sH.set(num, sH.get(num) + 1);
        else sH.set(num, 1);
    }

    for (let i = 0; i < arr.length; i++) {
        if (sH.has(i) && sH.get(i) !== arr[i] || !sH.has(i) && arr[i] !== 0) {
            return false;
        }
    }

    return true;
};
profile
Front-End Developer

0개의 댓글