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] <= 1060 <= digit <= 9Problem 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.
Solutions for this problem are being prepared.
Try solving it yourself| 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 |
Practice Count Digit Appearances with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor