Count Numbers with Non-Decreasing Digits - Problem
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 10⁹ + 7.
Input & Output
Example 1 — Basic Range in Base 10
$
Input:
l = "12", r = "15", b = 10
›
Output:
3
💡 Note:
Numbers 12, 13, 15 have non-decreasing digits. 14 is invalid because 4 < 1.
Example 2 — Small Range in Base 2
$
Input:
l = "1", r = "5", b = 2
›
Output:
4
💡 Note:
In base 2: 1→1, 2→10, 3→11, 4→100, 5→101. Valid: 1, 11, 100, 101 (count = 4)
Example 3 — Single Number
$
Input:
l = "123", r = "123", b = 10
›
Output:
1
💡 Note:
Only number 123 in range, digits 1≤2≤3 so it's valid
Constraints
- 1 ≤ l ≤ r ≤ 1018
- 2 ≤ b ≤ 10
- l and r are given as strings
Visualization
Tap to expand
Understanding the Visualization
1
Input Range
Given range [12, 15] in base 10
2
Check Digits
Verify each number has non-decreasing digits
3
Count Valid
Return count of valid numbers
Key Takeaway
🎯 Key Insight: Use digit DP to count valid numbers efficiently without checking each one individually
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code