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

In this guide, you will get 2283. Check if Number Has Equal Digit Count and Digit Value LeetCode Solution with the best time and space complexity. The solution to Check if Number Has Equal Digit Count and Digit Value problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if Number Has Equal Digit Count and Digit Value solution in C++
  4. Check if Number Has Equal Digit Count and Digit Value solution in Java
  5. Check if Number Has Equal Digit Count and Digit Value solution in Python
  6. Additional Resources
2283. Check if Number Has Equal Digit Count and Digit Value LeetCode Solution image

Problem Statement of 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.

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.

See also  1578. Minimum Time to Make Rope Colorful LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(10) = O(1)

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

class Solution {
 public:
  bool digitCount(string num) {
    vector<int> count(10);

    for (const char c : num)
      ++count[c - '0'];

    for (int i = 0; i < num.length(); ++i)
      if (count[i] != num[i] - '0')
        return false;

    return true;
  }
};
/* code provided by PROGIEZ */

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

class Solution {
  public boolean digitCount(String num) {
    int[] count = new int[10];

    for (final char c : num.toCharArray())
      ++count[c - '0'];

    for (int i = 0; i < num.length(); ++i)
      if (count[i] != num.charAt(i) - '0')
        return false;

    return true;
  }
}
// code provided by PROGIEZ

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

class Solution:
  def digitCount(self, num: str) -> bool:
    count = collections.Counter(num)
    return all(count[str(i)] == int(digit)
               for i, digit in enumerate(num))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.