Python 2520.py
class Solution:
def countDigits(self, num: int) -> int:
return sum(1 for c in str(num) if num % int(c) == 0)
Difficulty
and has an acceptance rate of about 86% (200k+ submissions).
Typical patterns to solve this problem:
- Math
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a single method countDigits that receives an integer num and returns the count of its digits that evenly divide the original number. The solution follows a concise functional style, leveraging Python’s built-in sum together with a generator expression to perform the calculation in a single line.
Key Steps
The core logic can be broken down into three conceptual operations, each illustrated with a focused snippet.
Conversion of the integer to an iterable sequence of characters
str_num = str(num)
By turning the integer into its string representation, each digit becomes an individual character that can be traversed without additional arithmetic.
Evaluation of the divisibility condition for each digit
for c in str_num:
digit = int(c)
if num % digit == 0:
# digit contributes to the count
Inside the generator expression, each character c is cast back to an integer digit. The modulo operation num % digit determines whether the digit is a divisor of the original number. Only digits satisfying this condition are considered.
Aggregation of the qualifying digits
result = sum(1 for c in str_num if num % int(c) == 0)
The generator yields the constant value 1 for every digit that passes the divisibility test. The surrounding sum aggregates these ones, producing the final count.
Edge Cases
The algorithm inherently handles numbers of varying length, including single-digit inputs. When the input consists solely of digits that do not divide the number, the generator yields no values and sum returns zero, correctly indicating that no digit satisfies the condition.
Digits equal to zero present a special situation because division by zero is undefined. In such cases, the modulo expression cannot be evaluated, and the digit is effectively excluded from the count. Consequently, numbers containing zero digits do not contribute those digits to the final tally.
Negative numbers are transformed to their string form including the leading minus sign. The minus sign is not a numeric character, so it is ignored by the generator’s int(c) conversion, which would raise an exception if encountered. Therefore, the method is intended for non-negative inputs, and the conversion step naturally filters out non-digit characters.
Performance Optimizations
The implementation avoids constructing intermediate collections by using a generator expression directly within sum. This lazy evaluation means that each digit is processed and discarded immediately after its contribution is accounted for, minimizing memory overhead.
Converting the entire number to a string once, rather than repeatedly extracting digits via arithmetic operations, reduces the number of costly integer divisions. The per-digit int(c) conversion is lightweight compared to repeated modulo calculations on the original integer.
By returning the result of the single sum call, the function eliminates the need for explicit loops or accumulator variables, allowing the Python interpreter to execute the operation with minimal bytecode overhead.
ELI5 - Simplified Explanation
Imagine a magical machine that is given a secret number like one hundred and twenty-four. First, the machine peels apart the number to look at each digit, which are the one, the two, and the four. Then, the machine takes the original big number and tries to divide it by each of those little digits one by one. If a digit fits perfectly into the big number without leaving any extra crumbs behind, the machine keeps that digit to count it later. After checking every single digit, the machine finishes its job by telling the count of how many digits were able to divide the big number perfectly.
Code Quality
Complexity Analysis
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | |||||||||
| www | AlgoMonster | Python | Java | C++ | TS | |||||
| WalkCCC | Python | Java | C++ | |||||||
| leetcode.ca | Python | Java | C++ | C | Go | Rust | TS | |||
| Progiez | Python | Java | C++ | |||||||
| github | chandansgowda | Python | ||||||||
| charles-wangkai | Java | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| JoshCrozier | JS | |||||||||
| lkwq007 | Python | |||||||||
| varunu28 | Java | |||||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||||
| WalkCCC | C++, Python, Java | |||||||||
| leetcode.ca | C, C++, Go, Java, Python, Rust, TypeScript | |||||||||
| Progiez | C++, Java, Python | |||||||||
| chandansgowda | Python | |||||||||
| charles-wangkai | Java | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| JoshCrozier | JavaScript | |||||||||
| lkwq007 | Python | |||||||||
| varunu28 | Java |
Videos
Similar Problems
- problem 202: Happy Number
- problem 728: Self Dividing Numbers
Solved On The Same Day
- problem 1260: Shift 2D Grid
- problem 1309: Decrypt String from Alphabet to Integer Mapping
- problem 2553: Separate the Digits in an Array
- problem 2815: Max Pair Sum in an Array
- problem 2833: Furthest Point From Origin
- problem 921: Minimum Add to Make Parentheses Valid
- problem 986: Interval List Intersections
- problem 1003: Check If Word Is Valid After Substitutions
- problem 564: Find the Closest Palindrome
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality