We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Count the Digits That Divide a Number

Difficulty: Easy


Problem Description

Given an integer num, return the number of digits in num that divide num. An integer val divides num if num % val == 0.


Key Insights

  • We need to iterate through each digit of the number num.
  • For each digit, we check if it divides num without leaving a remainder.
  • Count the digits that satisfy the division condition.
  • The input number is guaranteed to be between 1 and 1,000,000,000 and will not contain the digit '0'.

Space and Time Complexity

Time Complexity: O(d), where d is the number of digits in num (at most 10 digits).
Space Complexity: O(1), since we are using a constant amount of space.


Solution

To solve the problem, we will use a simple algorithm that involves:

  1. Converting the integer num to a string to easily iterate through each digit.
  2. For each digit, convert it back to an integer and check if it divides num using the modulus operator.
  3. Maintain a count of how many digits divide num and return this count at the end.

Code Solutions

def countDigits(num):
    count = 0
    for digit in str(num):
        if num % int(digit) == 0:
            count += 1
    return count
← Back to All Questions