Digit Count in Range - Problem

You're working with digit analysis! Given a single-digit integer d (from 0-9) and two integers low and high, your task is to count how many times the digit d appears in all numbers within the inclusive range [low, high].

For example, if you're looking for digit 1 in the range [1, 13], you need to examine each number: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13. The digit 1 appears in: 1 (once), 10 (once), 11 (twice), 12 (once), 13 (once) = 6 times total.

Note: Multi-digit numbers can contain the target digit multiple times (like 11 contains two 1's), and each occurrence should be counted separately.

Input & Output

example_1.py โ€” Basic Range
$ Input: d = 1, low = 1, high = 13
โ€บ Output: 6
๐Ÿ’ก Note: Numbers: 1(1 occurrence), 10(1), 11(2), 12(1), 13(1). Total = 6 occurrences of digit '1'.
example_2.py โ€” Zero Digit
$ Input: d = 0, low = 10, high = 20
โ€บ Output: 2
๐Ÿ’ก Note: Only numbers 10 and 20 contain digit '0' in this range, each contributing 1 occurrence.
example_3.py โ€” Large Range
$ Input: d = 3, low = 100, high = 250
โ€บ Output: 66
๐Ÿ’ก Note: Digit '3' appears in units, tens, and hundreds places across the range [100, 250].

Visualization

Tap to expand
Digit Count VisualizationExample: Count digit '1' in range [10, 15]10Count: 111Count: 212Count: 113Count: 114Count: 115Count: 1Total Occurrences: 1 + 2 + 1 + 1 + 1 + 1 = 7Position-wise Analysis for Mathematical Approach:Units Place (1s)Pattern: 1, 11, 21, 31...Count: 6 occurrencesEvery 10 numbersTens Place (10s)Pattern: 10-19, 110-119...Count: 1 occurrence10 consecutive numbers๐ŸŽฏ Mathematical DP avoids checking each number individually!
Understanding the Visualization
1
Identify the Problem
We need to count digit 'd' in all numbers from low to high
2
Brute Force Approach
Check each number individually - simple but slow
3
Mathematical Insight
Calculate contributions by digit position instead of individual numbers
4
Optimize with DP
Use digit DP formula to count efficiently in O(log n) time
Key Takeaway
๐ŸŽฏ Key Insight: Instead of brute force iteration, use mathematical formulas to calculate digit contributions by position. This transforms O(n ร— log n) brute force into O(log n) optimal solution, making it scalable for very large ranges.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * log n)

We iterate through n numbers, and for each number we need O(log n) time to process its digits

n
2n
โšก Linearithmic
Space Complexity
O(log n)

Space needed to store string representation of largest number

n
2n
โšก Linearithmic Space

Constraints

  • 0 โ‰ค d โ‰ค 9 (single digit)
  • 1 โ‰ค low โ‰ค high โ‰ค 2 ร— 108
  • Note: For digit 0, leading zeros are not counted
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
27.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen