[코테 풀이] Check if Number Has Equal Digit Count and Digit Value

시내·2024년 7월 15일
0

Q_2283) Check if Number Has Equal Digit Count and Digit Value

출처 : https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/

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.

class Solution {
    public boolean digitCount(String num) {
        int ind = 0;
        for (int i = 0; i < num.length(); i++) {
            if (num.charAt(ind)-'0' != count(num, ind)) {
                return false;
            }
            ind++;
        }
        return true;
    }

    public int count(String num, int n) {
        int count = 0;
        for (int i = 0; i < num.length(); i++) {
            if (num.charAt(i) - '0' == n) count++;
        }
        return count;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글