Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Numbers At Most N Given Digit Set

XPChallenge Points: 30
levelLevel: Hard

You are given a sorted list of characters representing digits. You can form numbers by using these digits freely, and each digit may be used multiple times.

Your task is to determine how many positive integers can be created using only these digits such that the resulting number is less than or equal to a given integer n.

You may generate numbers of any length (1 digit, 2 digits, 3 digits, etc.), as long as the number does not exceed n.

Example 1:

Input: digits = ["1","5"], n = 60

Output: 6

Explanation:

Valid numbers: 1,5,11,15,51,55.

Example 2:

Input: digits = ["2","4","8"], n = 500

Output: 15

Explanation:

All valid numbers include: 2,4,8,22,24,28,42,44,48,82,84,88,222,224,228 → total 15.

Example 3:

Input: digits = ["3","9"], n = 3

Output: 1

Explanation:

Only "3" ≤ 3.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result