리트코드
[LeetCode] 2283. Check if Number Has Equal Digit Count and Digit Value
킴준현
2025. 4. 9. 11:49
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/description/
✅ 문제
📌 접근방법
0부터 9까지 숫자가 몇 개가 나오는 지 기록할 배열이 필요하다.
입력값 속 숫자를 기록한 뒤, 서로 비교해서 다르면 false, 같으면 true
🔑 풀이
class Solution {
public boolean digitCount(String num) {
int[] count = new int[10]; // 0~9까지 숫자 빈도 카운트 배열
char[] text = num.toCharArray(); // 문자열을 문자 배열에 저장
// 1. 각 숫자 등장 횟수 세기
for(char c : text) {
count[c - '0']++; // 문자 '0' 빼서 정수화
}
for (int i = 0; i < num.length(); i++) {
if(count[i] != num.charAt(i) - '0') { // 비교
return false; // 다르면 false
}
}
return true; // 일치하면 true
}
}