Talentd
FleetCode

1067. Digit Count in Range

Hard46.4% acceptance1 companyPremium elsewhere, free here

Given a single-digit integer d and two integers low and high, return the number of times that d occurs as a digit in all integers in the inclusive range [low, high].

Example 1:

Input: d = 1, low = 1, high = 13
Output: 6
Explanation: The digit d = 1 occurs 6 times in 1, 10, 11, 12, 13.
Note that the digit d = 1 occurs twice in the number 11.

Example 2:

Input: d = 3, low = 100, high = 250
Output: 35
Explanation: The digit d = 3 occurs 35 times in 103,113,123,130,131,...,238,239,243.

Constraints:

  • 0 <= d <= 9
  • 1 <= low <= high <= 2 * 108

Asked at 1 company

Hints open one at a time

Hint 1
Define a function f(x) to get the requested sum from 1 to x. So the answer will be f(hi) - f(lo - 1)
Hint 2
In order to solve f(x) we need to do a DP over digits approach.
26:35
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
1
1
13
Input
3
100
250
Your code is saved on this device.Solution