Count the Number of Powerful Integers - Problem
You are given three integers start, finish, and limit. You are also given a 0-indexed string s representing a positive integer.
A positive integer x is called powerful if it ends with s (in other words, s is a suffix of x) and each digit in x is at most limit.
Return the total number of powerful integers in the range [start, finish].
A string x is a suffix of a string y if and only if x is a substring of y that starts from some index (including 0) in y and extends to the index y.length - 1. For example, 25 is a suffix of 5125 whereas 512 is not.
Input & Output
Example 1 — Basic Case
$
Input:
start = 1, finish = 15, limit = 2, s = "2"
›
Output:
2
💡 Note:
The powerful integers are 2 and 12. Both end with "2" and have all digits ≤ 2. Numbers like 22 are > 15, and 32 has digit 3 > 2.
Example 2 — No Valid Numbers
$
Input:
start = 1, finish = 15, limit = 1, s = "2"
›
Output:
0
💡 Note:
No numbers ending with "2" can have all digits ≤ 1, since "2" itself contains digit 2 > 1.
Example 3 — Larger Suffix
$
Input:
start = 1000, finish = 2000, limit = 4, s = "124"
›
Output:
1
💡 Note:
Only 1124 is in range [1000,2000], ends with "124", and has all digits ≤ 4.
Constraints
- 1 ≤ start ≤ finish ≤ 1015
- 1 ≤ limit ≤ 9
- 1 ≤ s.length ≤ floor(log10(finish)) + 1
- s only contains digits, and doesn't have leading zeros
Visualization
Tap to expand
Understanding the Visualization
1
Input Range
Numbers from start=1 to finish=15, limit=2, suffix="2"
2
Filter Process
Check each number for suffix and digit constraints
3
Count Result
Count valid powerful integers: 2
Key Takeaway
🎯 Key Insight: Use digit DP to efficiently count valid numbers without enumerating them all
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code