PLOD

[Leetcode] 2283. Check if Number Has Equal Digit Count and Digit Value 본문

대외 활동 및 IT 지식/알고리즘 문제 풀이 정리

[Leetcode] 2283. Check if Number Has Equal Digit Count and Digit Value

훌룽이 2026. 3. 20. 22:06

🔗 문제 링크

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

 

Check if Number Has Equal Digit Count and Digit Value - LeetCode

Can you solve this real interview question? 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] t

leetcode.com

 

✅ 코드

import java.util.*;

class Solution {
    public boolean digitCount(String num) {
        boolean answer = true;

        char[] numCharList = num.toCharArray();

        for(int i = 0; i < numCharList.length; i++){
            char n = (char)(i + '0');
            int cnt = 0;
            System.out.println(n);
            for(int j = 0; j < numCharList.length; j++){
                if(numCharList[j] == n){
                    cnt++;
                }
            }
            
            if(cnt != numCharList[i] - '0'){
                answer = false;
                break;
            }
        }

        return answer;
    }
}

💡 배운 점 & 느낀 점

1. int → char : 문자에 '0'을 더해주는 방법으로 타입 캐스팅을 한다

int num = 3;
char numChar = (char)(num+'0');

 

2. char → int : 숫자에 '0'을 빼주는 방법으로 타입 캐스팅을 한다

char newChar = 'A'
int newInt = newChar - '0'
Comments