Skip to content
AI360Xpert
Beta
Difficulty: Hard1-D Dynamic Programming

Numbers At Most N Given Digit Set

Problem in Plain English

Count positive integers at most n whose decimal digits all belong to a supplied set. Each allowed digit can be reused, but zero is not in the set.

Problem Statement

Given a sorted array digits of distinct one-character strings from "1" through "9" and a positive integer n, return how many positive integers at most n can be written using only those digits. Each digit can be reused any number of times.

The official statement excludes zero from the allowed set. Leading positions in a fixed-width DP are bookkeeping for shorter numbers, not digits of a generated number.

Constraints

  • 1 <= digits.length <= 9 and digits[i].length == 1.
  • Each digit is from "1" to "9"; entries are unique and sorted in nondecreasing order.
  • 1 <= n <= 10^9.

Examples

Example 1

Input
digits = ["1","3","5","7"], n = 100
Output
20

There are four one-digit numbers and sixteen two-digit numbers, all below 100. No three-digit number is valid: the only possible first digit under this bound is 1, then the next allowed digit exceeds 0.

Example 2

Input
digits = ["1","4","9"], n = 1000000000
Output
29523

All valid lengths from one through nine fit: 3 + 9 + 27 + 81 + 243 + 729 + 2187 + 6561 + 19683 = 29523. A ten-digit candidate cannot follow the bound’s first 1 with zero.

Example 3

Input
digits = ["7"], n = 8
Output
1

Only 7 fits. The all-empty leading-position choice denotes zero and is excluded.

Example 4

Input
digits = ["1","3"], n = 13
Output
4

The valid numbers are 1, 3, 11 and 13. The last is equal to the bound and must be included; 31 and 33 are too large.

Intuition

Compare numbers to the decimal representation of the bound one position at a time. A prefix equal to the bound remains tight, meaning its next digit cannot exceed the corresponding bound digit; a smaller prefix is free to use any allowed digit. A separate started flag says whether a real digit has appeared. In Example 1, with bound 100, skipping the first position makes a shorter number; choosing 1 immediately keeps the prefix tight but then cannot match or beat the next zero. Skipping leading positions once per position gives each shorter positive number exactly one padded representation. Skip the all-empty representation so zero is never counted.

Approaches

Digit DP

Optimal

Solution Details

Reveal Digit DP: intuition, complexity, and code

Hints

Hint 1
Which prefixes still need to respect the remaining digits of n?
Hint 2
Track position, whether the prefix equals n’s prefix, and whether a real digit has started.
Hint 3
Allow skips only before starting; terminal unstarted states count zero. Memoize and sum all legal next digits.

Edge Cases

  • If every allowed digit exceeds a one-digit bound, the answer is zero.
  • When every digit of n is allowed, the tight equality branch includes n itself.
  • At a power-of-ten bound, zero is unavailable, so no full-length candidate can finish the tight prefix.
  • A singleton set still permits repeated digits such as 7, 77 and 777.

Common Mistakes and Interview Tips

  • Counting the terminal unstarted state includes zero, which is not a positive integer.
  • Allowing a skip after starting invents gaps and counts the same shorter number multiple times.
  • Keeping tight after a leading skip constrains a shorter number using unrelated bound digits.
  • Adding a shorter-length combinatorial count to this started-state DP counts shorter numbers twice.

Key Takeaway

Digit DP groups prefixes by their effect on future digits. Tightness enforces an inclusive bound; leading-position state gives shorter numbers a unique representation and excludes zero explicitly.