Problem

Given an integer num, return _the number of digits innum that divide _num.

An integer val divides nums if nums % val == 0.

Examples

Example 1

1
2
3
Input: num = 7
Output: 1
Explanation: 7 divides itself, hence the answer is 1.

Example 2

1
2
3
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

1
2
3
Input: num = 1248
Output: 4
Explanation: 1248 is divisible by all of its digits, hence the answer is 4.

Constraints

  • 1 <= num <= 10^9
  • num does not contain 0 as one of its digits.

Solution

Method 1 – Digit Check and Count

Intuition

For each digit in the number, check if it divides the number. Since the number does not contain 0 as a digit, we can safely check divisibility for each digit.

Approach

  1. Convert the number to a string or process its digits one by one.
  2. For each digit, check if num % digit == 0.
  3. Count the number of such digits.
  4. Return the count.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    int countDigits(int num) {
        int ans = 0, n = num;
        while (n > 0) {
            int d = n % 10;
            if (num % d == 0) ans++;
            n /= 10;
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
type Solution struct{}
func (Solution) CountDigits(num int) int {
    ans, n := 0, num
    for n > 0 {
        d := n % 10
        if num%d == 0 {
            ans++
        }
        n /= 10
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    public int countDigits(int num) {
        int ans = 0, n = num;
        while (n > 0) {
            int d = n % 10;
            if (num % d == 0) ans++;
            n /= 10;
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    fun countDigits(num: Int): Int {
        var ans = 0
        var n = num
        while (n > 0) {
            val d = n % 10
            if (num % d == 0) ans++
            n /= 10
        }
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
    def countDigits(self, num: int) -> int:
        ans = 0
        n = num
        while n > 0:
            d = n % 10
            if num % d == 0:
                ans += 1
            n //= 10
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
impl Solution {
    pub fn count_digits(num: i32) -> i32 {
        let mut ans = 0;
        let mut n = num;
        while n > 0 {
            let d = n % 10;
            if num % d == 0 {
                ans += 1;
            }
            n /= 10;
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    countDigits(num: number): number {
        let ans = 0, n = num;
        while (n > 0) {
            const d = n % 10;
            if (num % d === 0) ans++;
            n = Math.floor(n / 10);
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(log_{10} n), since we process each digit of the number.
  • 🧺 Space complexity: O(1), only a few variables are used.