출처 : 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;
}
}