Count Digit Appearances - Solution & Explanation
Problem Statement
You are given an integer array nums and an integer digit.
Return the total number of times digit appears in the decimal representation of all elements in nums.
Example 1:
Input: nums = [12,54,32,22], digit = 2
Output: 4
Explanation:
The digit 2 appears once in 12 and 32, and twice in 22. Thus, the total number of times digit 2 appears is 4.
Example 2:
Input: nums = [1,34,7], digit = 9
Output: 0
Explanation:
The digit 9 does not appear in the decimal representation of any element in nums, so the total number of times digit 9 appears is 0.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 106​​​​​​​0 <= digit <= 9
Approach Overview
Problem Overview: You receive an integer n and a digit d. The goal is to count how many times digit d appears in the decimal representation of all numbers from 0 to n.
Approach 1: Brute Force Enumeration (O(n log n) time, O(1) space)
The most direct solution iterates through every number from 0 to n. Convert each number to a string or repeatedly extract digits using % 10 and / 10. For every digit encountered, compare it with d and increment the counter. Each number has at most log10(n) digits, which leads to O(n log n) total work. This approach is useful for understanding the problem but becomes slow when n grows large.
Approach 2: Positional Digit Counting (O(log n) time, O(1) space)
A faster method analyzes each digit position independently. For a position value factor = 1, 10, 100..., split the number into three parts: higher, current, and lower. These represent the digits left of the position, the digit at the position, and the digits to the right. Using these values, you can determine how many full cycles of the digit occur at that position and adjust based on the current digit. Iterate through all positions until factor > n. Since the number of positions is log10(n), the algorithm runs in O(log n) time with constant memory.
Approach 3: Digit Dynamic Programming (O(d * 10) time, O(d) space)
For generalized counting problems—especially when constraints include digit limits or multiple queries—digit DP provides a structured approach. Traverse the digits of n from most significant to least significant while tracking whether the current prefix is restricted by the upper bound. Maintain state for the position and the number of occurrences counted so far. Although heavier than the positional formula, it easily extends to variations such as counting digits under additional constraints.
Recommended for interviews: Start by explaining the brute force scan to show understanding of the digit counting process. Then transition to the positional math approach. Interviewers usually expect the O(log n) solution because it demonstrates number decomposition and efficient counting. If the problem expands with constraints or ranges, mentioning mathematical digit analysis or digit DP shows deeper problem‑solving range.
Solution
We traverse each element in the array and count how many times digit appears. For each element, we can obtain each of its digits by repeatedly taking the modulo and dividing by 10, and compare each digit with digit. If they are equal, we increment the answer by 1.
Finally, return the answer.
The time complexity is O(n times log_{10} M), and the space complexity is O(1). Here, n and M are the length of the array and the maximum value in the array, respectively.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n log n) | O(1) | Small n or quick verification of logic |
| Positional Digit Counting | O(log n) | O(1) | General case and expected interview solution |
| Digit Dynamic Programming | O(d * 10) | O(d) | When additional constraints or multiple queries are involved |
Video Solution
Count Digit Appearances | Leetcode 3895 | Explanation With Code | Java • codewithsitaram • 63 views views
Watch 3 more video solutions →Frequently Asked Questions
Is Count Digit Appearances easy or hard?
Count Digit Appearances Python/Java solution
How to solve Count Digit Appearances in O(log n)?
What is the best approach for Count Digit Appearances?
Is Count Digit Appearances asked at Google/Amazon/Meta?
What data structure is used in Count Digit Appearances?
What is the time complexity of Count Digit Appearances?
Ready to solve this problem?
Practice Count Digit Appearances with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor