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

Problem – Check if Number Has Equal Digit Count and Digit Value LeetCode Solution

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.

Example 1:

Input: num = "1210"
Output: true
Explanation:
num[0] = '1'. The digit 0 occurs once in num.
num[1] = '2'. The digit 1 occurs twice in num.
num[2] = '1'. The digit 2 occurs once in num.
num[3] = '0'. The digit 3 occurs zero times in num.
The condition holds true for every index in "1210", so return true.

Example 2:

Input: num = "030"
Output: false
Explanation:
num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num.
num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num.
num[2] = '0'. The digit 2 occurs zero times in num.
The indices 0 and 1 both violate the condition, so return false.

Constraints:

  • n == num.length
  • 1 <= n <= 10
  • num consists of digits.

Check if Number Has Equal Digit Count and Digit Value LeetCode Solution in Java

class Solution {
    public boolean digitCount(String num) {
        int[] freqArr = new int[10];  // n = 10 given in constraints;
        
        
        for(char ch : num.toCharArray()){
            freqArr[ch-'0']++;
        }
        
        for(int i=0;i<num.length();i++){
            int freq = num.charAt(i)-'0';  //freq of each indexValue;
            freqArr[i] = freqArr[i] - freq; 
        }
        for(int i=0;i<10;i++){
            if(freqArr[i]!=0){
                return false;
            }
        }
        return true;
    }
}

Check if Number Has Equal Digit Count and Digit Value LeetCode Solution in C++

class Solution {
public:
    bool digitCount(string num) {
        unordered_map<char, int> mpp;
        
        for(int i=0; i<num.size(); i++)
            mpp[num[i]]++;
        

        for(int i=0; i<num.size(); i++){
            char c ='0' + i;
            // cout<<c<<endl;
            if(num[i] != '0' + mpp[c]){
                // cout<<num[i]<<" "<<mpp[c]<<endl;
                return false;
            }
                
        }
        
        return true;
    }
};

Check if Number Has Equal Digit Count and Digit Value LeetCode Solution in Python

class Solution:
    def digitCount(self, num: str) -> bool:
        n=len(num)
        
        for i in range(n):
            if(num.count(str(i))!=int(num[i])):
                return False
            
        return True
Check if Number Has Equal Digit Count and Digit Value LeetCode Solution Review:

In our experience, we suggest you solve this Check if Number Has Equal Digit Count and Digit Value LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Check if Number Has Equal Digit Count and Digit Value LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Check if Number Has Equal Digit Count and Digit Value LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.