Skip to content
DeepForgeby svx
All problems
HardAlgorithmsal-297

Numbers at Most N Given Digit Set

Problem

Given a set of allowed digit characters and a bound n, count the positive integers less than or equal to n that can be written using only those digits, with no leading zeros.

Count all valid numbers with fewer digits than n, then walk the digits of n from the left, adding the numbers that are smaller at each position and stopping when the current digit is not allowed.

Hint

For a prefix that matches n exactly, any smaller allowed digit at the next position is safe.

Test cases

case 1
in: [["1","3","5","7"],100]
expected: 20
case 2
in: [["1","4","9"],1000000000]
expected: 29523
case 3
in: [["7"],8]
expected: 1
case 4
in: [["1"],1]
expected: 1
case 5
in: [["2","4"],5]
expected: 2
Starter codePython
def at_most_n_digit_set(digits, n):
    # Your code here
    pass

5 test cases · run your code against them in the editor