# Count the Digits That Divide a Number
**Difficulty:** EASY
[External](https://leetcode.com/problems/count-the-digits-that-divide-a-number)
Canonical: https://scaleengineer.com/dsa/problems/count-the-digits-that-divide-a-number
**Patterns:** [Math](https://scaleengineer.com/dsa/patterns/math)
**Companies:** [tcs](https://scaleengineer.com/companies/tcs)
---
## Problem
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.

# Approaches
## Approach 1: String Conversion and Iteration
This approach involves converting the integer `num` into its string representation. Once we have the string, we can easily iterate through each character, which represents a digit. For each character, we convert it back to an integer and check if it divides the original number `num`.
**Time:** O(log10(num)) · **Space:** O(log10(num))
**Pros:** The logic is very straightforward and easy to read and implement.; Leverages built-in language features for string conversion and iteration.
**Cons:** Uses extra space proportional to the number of digits to store the string representation.; Involves type conversions (integer to string, character to integer) which can have a minor performance overhead compared to pure arithmetic.
### Explanation
The core idea is to leverage the simplicity of string manipulation to access the individual digits of the number. By converting the number to a string, we can use a simple loop to go through each digit one by one.

```java
class Solution {
    public int countDigits(int num) {
        String numStr = Integer.toString(num);
        int count = 0;
        for (char c : numStr.toCharArray()) {
            int digit = c - '0';
            // According to constraints, num does not contain 0 as a digit,
            // so we don't need to check for division by zero.
            if (num % digit == 0) {
                count++;
            }
        }
        return count;
    }
}
```
### Algorithm
- Initialize a counter `count` to 0.
- Convert the input integer `num` to its string representation, let's call it `s`.
- Iterate through each character `c` in the string `s`.
- For each character, convert it back to its integer value `digit`. In Java, this is `c - '0'`.
- Check if the original `num` is divisible by `digit` (i.e., `num % digit == 0`).
- If it is, increment the `count`.
- After the loop finishes, return the `count`.

## Approach 2: Iterative Mathematical Approach
This approach extracts digits from the number using arithmetic operations, specifically the modulo (`%`) and division (`/`) operators. It avoids converting the number to a string, making it more efficient in terms of memory usage.
**Time:** O(log10(num)) · **Space:** O(1)
**Pros:** Extremely memory efficient, using only a constant amount of extra space.; Avoids the overhead of creating string objects and performing type conversions.; Generally faster due to direct use of low-level arithmetic operations.
**Cons:** The logic of using modulo and division to extract digits might be slightly less intuitive for beginners compared to string manipulation.
### Explanation
Instead of converting the number to a different data type, we can work with it directly using mathematics. The last digit of any integer can be found by `number % 10`. The last digit can be removed by `number / 10` (integer division). We can repeat this process in a loop until the number becomes zero, effectively processing all its digits.

```java
class Solution {
    public int countDigits(int num) {
        int count = 0;
        int temp = num;
        while (temp > 0) {
            int digit = temp % 10;
            // According to constraints, num does not contain 0 as a digit,
            // so we don't need to check for division by zero.
            if (num % digit == 0) {
                count++;
            }
            temp /= 10;
        }
        return count;
    }
}
```
### Algorithm
- Initialize a counter `count` to 0.
- Create a temporary variable `temp` and assign it the value of `num`. This preserves the original `num` for the divisibility checks.
- Start a `while` loop that continues as long as `temp` is greater than 0.
- Inside the loop, extract the last digit of `temp` using the modulo operator: `digit = temp % 10`.
- Check if the original number `num` is divisible by `digit`.
- If `num % digit == 0`, increment the `count`.
- Remove the last digit from `temp` by integer division: `temp = temp / 10`.
- After the loop terminates (when `temp` becomes 0), return the final `count`.

# Solutions
### Java

```java
class Solution { public int countDigits ( int num ) { int ans = 0 ; for ( int x = num ; x > 0 ; x /= 10 ) { if ( num % ( x % 10 ) == 0 ) { ++ ans ; } } return ans ; } }
```

### CPP

```cpp
class Solution { public: int countDigits ( int num ) { int ans = 0 ; for ( int x = num ; x > 0 ; x /= 10 ) { if ( num % ( x % 10 ) == 0 ) { ++ ans ; } } return ans ; } };
```

### Python

```python
class Solution : def countDigits ( self , num : int ) -> int : ans , x = 0 , num while x : x , val = divmod ( x , 10 ) ans += num % val == 0 return ans
```
