Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1067.html
1067. Digit Count in Range
Level
Hard
Description
Given an integer d
between 0
and 9
, and two positive integers low
and high
as lower and upper bounds, respectively. Return the number of times that d
occurs as a digit in all integers between low
and high
, including the bounds low
and 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.
Note:
0 <= d <= 9
1 <= low <= high <= 2×10^8
Solution
Calculate the number of times that d
occurs as a digit in all integers between 1 and high
, and calculate the number of times that d
occurs as a digit in all integers between 1 and low - 1
. Then calculate the difference between the two numbers of times to obtain the result.
If d
is not 0, the number of times is calculated as follows.
- Loop over
i
from 1 ton
. Each time seti *= 10
. - The value
n / (i * 10) * i
represents the number of digitd
at digiti * 10
. - The value
Math.min(Math.max(n % (i * 10) - d * i + 1, 0), i)
represents the extra number of digitd
at digiti * 10
. - Calculate the number of times of
d
by calculating the sum of the values in the previous two steps.
If d
is 0, the number of times is calculated as follows.
- Loop over
i
from 1 ton / 10
. Each time seti *= 10
. - The value
(n / (i * 10) - 1) * i
represents the number of digitd
at digiti * 10
. - The value
Math.min(Math.max(n % (i * 10) - d * i + 1, 0), i)
represents the extra number of digitd
at digiti * 10
. - Calculate the number of times of
d
by calculating the sum of the values in the previous two steps.
The difference exists because digit 0 cannot appear at the highest digit, except the number 0. However, in this problem, both low
and high
are positive integers, so 0 is never in the range.
-
class Solution { public int digitsCount(int d, int low, int high) { long highCount = countDigits(high, d); long lowCount = countDigits(low - 1, d); int count = (int) (highCount - lowCount); return count; } public long countDigits(int n, int digit) { if (n <= 0) return 0; long count = 0; if (digit == 0) { for (long i = 1; i <= n / 10; i *= 10) { long unit = i * 10; count += (n / unit - 1) * i + Math.min(i, Math.max(0, n % unit - digit * i + 1)); } } else { for (long i = 1; i <= n; i *= 10) { long unit = i * 10; count += n / unit * i + Math.min(i, Math.max(0, n % unit - digit * i + 1)); } } return count; } }
-
class Solution: def digitsCount(self, d: int, low: int, high: int) -> int: return self.f(high, d) - self.f(low - 1, d) def f(self, n, d): @cache def dfs(pos, cnt, lead, limit): if pos <= 0: return cnt up = a[pos] if limit else 9 ans = 0 for i in range(up + 1): if i == 0 and lead: ans += dfs(pos - 1, cnt, lead, limit and i == up) else: ans += dfs(pos - 1, cnt + (i == d), False, limit and i == up) return ans a = [0] * 11 l = 0 while n: l += 1 a[l] = n % 10 n //= 10 return dfs(l, 0, True, True)
-
class Solution { public: int d; int a[11]; int dp[11][11]; int digitsCount(int d, int low, int high) { this->d = d; return f(high) - f(low - 1); } int f(int n) { memset(dp, -1, sizeof dp); int len = 0; while (n) { a[++len] = n % 10; n /= 10; } return dfs(len, 0, true, true); } int dfs(int pos, int cnt, bool lead, bool limit) { if (pos <= 0) { return cnt; } if (!lead && !limit && dp[pos][cnt] != -1) { return dp[pos][cnt]; } int up = limit ? a[pos] : 9; int ans = 0; for (int i = 0; i <= up; ++i) { if (i == 0 && lead) { ans += dfs(pos - 1, cnt, lead, limit && i == up); } else { ans += dfs(pos - 1, cnt + (i == d), false, limit && i == up); } } if (!lead && !limit) { dp[pos][cnt] = ans; } return ans; } };
-
func digitsCount(d int, low int, high int) int { f := func(n int) int { a := make([]int, 11) dp := make([][]int, 11) for i := range dp { dp[i] = make([]int, 11) for j := range dp[i] { dp[i][j] = -1 } } l := 0 for n > 0 { l++ a[l] = n % 10 n /= 10 } var dfs func(int, int, bool, bool) int dfs = func(pos, cnt int, lead, limit bool) int { if pos <= 0 { return cnt } if !lead && !limit && dp[pos][cnt] != -1 { return dp[pos][cnt] } up := 9 if limit { up = a[pos] } ans := 0 for i := 0; i <= up; i++ { if i == 0 && lead { ans += dfs(pos-1, cnt, lead, limit && i == up) } else { t := cnt if d == i { t++ } ans += dfs(pos-1, t, false, limit && i == up) } } if !lead && !limit { dp[pos][cnt] = ans } return ans } return dfs(l, 0, true, true) } return f(high) - f(low-1) }