![[LeetCode] 2283. Check if Number Has Equal Digit Count and Digit Value](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FpMuBC%2FbtsNd7jxlik%2F8obQLaHOo0Ri0WS7bK7QXK%2Fimg.png)
[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
}
}
'리트코드' 카테고리의 다른 글
[LeetCode] 225. Implement Stack using Queues (0) | 2025.04.04 |
---|---|
[LeetCode] 232. Implement Queue using Stacks (0) | 2025.04.03 |
@킴준현 :: 차근차근 꾸준히
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!