getCounts
that takes an integer num
and returns the count of digit d
up to num
.getCounts
, initialize res
(result) to 0, step
to 1 (representing the current digit position), and n
to 0 (representing the number formed by digits to the right of the current position).num
is greater than 0:
a. Calculate the current digit t
as num % 10
.
b. Update num
to num // 10
.
c. Depending on the value of t
relative to d
, update res
accordingly.
d. Update n
to include the current digit and step
to move to the next digit position.res
.digitsCount
calls getCounts(high) - getCounts(low - 1)
to get the final count of digit d
in the range [low, high]
.