You are given two integers, l and r, represented as strings, and an integer b. Return the count of integers in the inclusive range [l, r] whose digits are in non-decreasing order when represented in base b.
An integer is considered to have non-decreasing digits if, when read from left to right (from the most significant digit to the least significant digit), each digit is greater than or equal to the previous one.
Since the answer may be too large, return it modulo 109 + 7.
Example 1:
Input: l = "23", r = "28", b = 8
Output: 3
Explanation:
Example 2:
Input: l = "2", r = "7", b = 2
Output: 2
Explanation:
Constraints:
1 <= l.length <= r.length <= 1002 <= b <= 10l and r consist only of digits.l is less than or equal to the value represented by r.l and r do not contain leading zeros.Problem Overview: You need to count how many numbers satisfy the constraint that digits never decrease from left to right (for example, 11239 is valid while 132 is not). The challenge is that the numeric range can be large, so enumerating every value is too slow.
Approach 1: Brute Force Enumeration (O(n * d) time, O(1) space)
The most direct idea is to iterate through every number in the range and check whether its digits are non-decreasing. Convert each number to a string (or repeatedly extract digits with modulo) and verify that digit[i] >= digit[i-1]. Each validation costs O(d) where d is the number of digits. This works for very small ranges but quickly becomes infeasible when n grows to millions or higher.
Approach 2: Combinatorics with Stars and Bars (O(d * 10) time, O(1) space)
Non‑decreasing numbers have a key property: digits behave like a multiset chosen in sorted order. Counting them is equivalent to choosing digits with repetition allowed while maintaining order. Using combinatorics, the count of length d sequences with digits 0–9 in non‑decreasing order can be computed using combinations such as C(d + 9, 9). This method works well when the task is to count all valid numbers of a fixed length. However, it becomes tricky when an upper bound is imposed, because you must ensure the constructed numbers do not exceed that limit.
Approach 3: Digit Dynamic Programming (Digit DP) (O(d * 10 * 10) time, O(d * 10 * 2) space)
The optimal solution uses digit DP, a common technique for counting numbers with digit constraints. Process the number from left to right while tracking three states: the current index, the previous digit chosen, and whether the prefix is already smaller than the limit (tight flag). At each step, try digits from the previous digit up to the current bound, ensuring the non‑decreasing property holds. Memoization stores results for repeated states, reducing the search space dramatically.
This approach avoids enumerating all numbers. Instead, it enumerates only valid digit transitions. The complexity depends on digit length rather than the numeric value, which makes it efficient even for very large bounds.
Digit DP appears frequently in range-counting problems involving digit constraints such as monotonic digits, digit sums, or forbidden patterns. If you're new to this pattern, review dynamic programming, math combinatorics, and digit manipulation techniques often implemented through string representations.
Recommended for interviews: Start by describing the brute force validator to show understanding of the property. Then transition to digit DP as the scalable solution. Interviewers expect the DP formulation with (index, previousDigit, tight) state and memoization because it handles upper bounds efficiently while enforcing the non-decreasing constraint.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Digit Check | O(n * d) | O(1) | Small ranges or quick validation logic |
| Combinatorics Counting | O(d * 10) | O(1) | Counting all valid numbers of a fixed digit length |
| Digit Dynamic Programming | O(d * 10 * 10) | O(d * 10 * 2) | General case with upper bounds or range queries |
3519. Count Numbers with Non-Decreasing Digits (Leetcode Hard) • Programming Live with Larry • 297 views views
Watch 2 more video solutions →Practice Count Numbers with Non-Decreasing Digits with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor