2520. Count the Digits That Divide a Number LeetCode Solution
In this guide, you will get 2520. Count the Digits That Divide a Number LeetCode Solution with the best time and space complexity. The solution to Count the Digits That Divide a Number 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
- Problem Statement
- Complexity Analysis
- Count the Digits That Divide a Number solution in C++
- Count the Digits That Divide a Number solution in Java
- Count the Digits That Divide a Number solution in Python
- Additional Resources

Problem Statement of Count the Digits That Divide a Number
Given an integer num, return the number of digits in num that divide num.
An integer val divides nums if nums % val == 0.
Example 1:
Input: num = 7
Output: 1
Explanation: 7 divides itself, hence the answer is 1.
Example 2:
Input: num = 121
Output: 2
Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.
Example 3:
Input: num = 1248
Output: 4
Explanation: 1248 is divisible by all of its digits, hence the answer is 4.
Constraints:
1 <= num <= 109
num does not contain 0 as one of its digits.
Complexity Analysis
- Time Complexity: O(\log\texttt{num})
- Space Complexity: O(1)
2520. Count the Digits That Divide a Number LeetCode Solution in C++
class Solution {
public:
int countDigits(int num) {
int ans = 0;
for (int n = num; n > 0; n /= 10)
if (num % (n % 10) == 0)
++ans;
return ans;
}
};
/* code provided by PROGIEZ */
2520. Count the Digits That Divide a Number LeetCode Solution in Java
class Solution {
public int countDigits(int num) {
int ans = 0;
for (int n = num; n > 0; n /= 10)
if (num % (n % 10) == 0)
++ans;
return ans;
}
}
// code provided by PROGIEZ
2520. Count the Digits That Divide a Number LeetCode Solution in Python
class Solution:
def countDigits(self, num: int) -> int:
return sum(num % int(d) == 0 for d in str(num))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.