Skip to main content
Back to problems
Leetcode
Medium
Dynamic Programming
Math
Number Theory
Strings
Google
Count The Number Of Powerful Integers

Count how many integers in a range satisfy digit-wise constraints on their decimal representation.

Acceptance 0%
Problem Statement

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 x is at most limit
  • the decimal representation of x ends with the string s

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 range
  • finish: upper bound of the range
  • limit: maximum allowed digit value
  • s: 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 <= finish
  • 1 <= limit <= 9
  • s contains only decimal digits
  • The answer may be large enough to require 64-bit arithmetic
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.