Count how many integers in a range satisfy digit-wise constraints on their decimal representation.
You are given two integers start and finish, a maximum allowed digit limit, and a string s representing a suffix.
Count the integers x such that:
start <= x <= finish- every decimal digit of
xis at mostlimit - the decimal representation of
xends with the strings
Return the number of such integers.
The intended challenge is to efficiently count valid numbers in a numeric range without iterating through every integer one by one.
Input Format
start: lower bound of the rangefinish: upper bound of the rangelimit: maximum allowed digit values: required suffix as a decimal string
Output Format
- Return a single integer: the count of integers in
[start, finish]that satisfy all conditions.
Constraints
0 <= start <= finish1 <= limit <= 9scontains only decimal digits- The answer may be large enough to require 64-bit arithmetic
Example 1
Input
start = 1, finish = 200, limit = 4, s = "2"
Output
10
Explanation
Valid numbers are 2, 12, 20, 21, 22, 32, 42, 102, 112, and 122.
Example 2
Input
start = 10, finish = 1000, limit = 5, s = "5"
Output
25
Explanation
Count all numbers in the range whose digits are all at most 5 and that end in 5.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.