Numbers At Most N Given Digit Set

Hard

Description

Given a set of digits and integer n, return the count of positive integers that can be formed using only given digits and are at most n.

Examples

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

20 valid numbers.

Input:digits = ["2"], n = 23
Output:2
Explanation:

With digit "2", the valid numbers are 2 and 22 (both ≤ 23). 222 exceeds the limit, so the answer is 2.

Input:digits = ["1","2","3"], n = 15
Output:6
Explanation:

Using digits ["1","2","3"], the valid numbers ≤ 15 are: 1, 2, 3, 11, 12, 13. Total: 6.

Constraints

  • 1 ≤ digits.length ≤ 9

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!