##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/902.html

# 902. Numbers At Most N Given Digit Set (Hard)

We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  (Note that '0' is not included.)

Now, we write numbers using these digits, using each digit as many times as we want.  For example, if D = {'1','3','5'}, we may write numbers such as '13', '551', '1351315'.

Return the number of positive integers that can be written (using the digits of D) that are less than or equal to N.

Example 1:

Input: D = ["1","3","5","7"], N = 100
Output: 20
Explanation:
The 20 numbers that can be written are:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.


Example 2:

Input: D = ["1","4","9"], N = 1000000000
Output: 29523
Explanation:
We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers,
81 four digit numbers, 243 five digit numbers, 729 six digit numbers,
2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers.
In total, this is 29523 integers that can be written using the digits of D.

Note:

1. D is a subset of digits '1'-'9' in sorted order.
2. 1 <= N <= 10^9

Companies:
Amazon

Related Topics:
Math, Dynamic Programming

## Solution 1.

• class Solution {
public int atMostNGivenDigitSet(String[] D, int N) {
int counts = 0;
int digitsCount = D.length;
int length = String.valueOf(N).length();
for (int i = 1; i < length; i++)
counts += (int) Math.pow(digitsCount, i);
for (int i = 1; i <= length; i++) {
int curDigit = N / (int) Math.pow(10, length - i) % 10;
int index = binarySearch(D, curDigit);
if (index >= 0) {
counts += index * (int) Math.pow(digitsCount, length - i);
if (i == length)
counts++;
} else {
int curCount = (-index - 1) * (int) Math.pow(digitsCount, length - i);
counts += curCount;
break;
}
}
return counts;
}

public int binarySearch(String[] digits, int target) {
int low = 0, high = digits.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
int digit = Integer.parseInt(digits[mid]);
if (digit == target)
return mid;
else if (digit > target)
high = mid - 1;
else
low = mid + 1;
}
return -low - 1;
}
}

############

class Solution {
private int[] a = new int[12];
private int[][] dp = new int[12][2];
private Set<Integer> s = new HashSet<>();

public int atMostNGivenDigitSet(String[] digits, int n) {
for (var e : dp) {
Arrays.fill(e, -1);
}
for (String d : digits) {
}
int len = 0;
while (n > 0) {
a[++len] = n % 10;
n /= 10;
}
return dfs(len, 1, true);
}

private int dfs(int pos, int lead, boolean limit) {
if (pos <= 0) {
}
}
int ans = 0;
int up = limit ? a[pos] : 9;
for (int i = 0; i <= up; ++i) {
if (i == 0 && lead == 1) {
ans += dfs(pos - 1, lead, limit && i == up);
} else if (s.contains(i)) {
ans += dfs(pos - 1, 0, limit && i == up);
}
}
if (!limit && lead == 0) {
}
return ans;
}
}

• // OJ: https://leetcode.com/problems/numbers-at-most-n-given-digit-set/
// Time: O(D)
// Space: O(D)
class Solution {
private:
int getCount(vector<string>& D, int N, int digits) {
int maxDigits = (int)log10(N) + 1;
if (digits > maxDigits) return 0;
if (digits < maxDigits) return pow(D.size(), digits);
int firstDigit = N / pow(10, maxDigits - 1), sum = 0;
for (auto &d : D) {
if (d[0] - '0' < firstDigit) sum += pow(D.size(), digits - 1);
else if (d[0] - '0' == firstDigit) sum += maxDigits == 1 ? 1 : getCount(D, N % (int)pow(10, maxDigits - 1), maxDigits - 1);
else break;
}
return sum;
}
public:
int atMostNGivenDigitSet(vector<string>& D, int N) {
int sum = 0, maxDigits = (int)log10(N) + 1;
for (int d = 1; d <= maxDigits; ++d) sum += getCount(D, N, d);
return sum;
}
};

• class Solution:
def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:
@cache
if pos <= 0:
up = a[pos] if limit else 9
ans = 0
for i in range(up + 1):
if i == 0 and lead:
ans += dfs(pos - 1, lead, limit and i == up)
elif i in s:
ans += dfs(pos - 1, False, limit and i == up)
return ans

l = 0
a = [0] * 12
s = {int(d) for d in digits}
while n:
l += 1
a[l] = n % 10
n //= 10
return dfs(l, True, True)


• func atMostNGivenDigitSet(digits []string, n int) int {
s := map[int]bool{}
for _, d := range digits {
i, _ := strconv.Atoi(d)
s[i] = true
}
a := make([]int, 12)
dp := make([][2]int, 12)
for i := range a {
dp[i] = [2]int{-1, -1}
}
l := 0
for n > 0 {
l++
a[l] = n % 10
n /= 10
}
var dfs func(int, int, bool) int
dfs = func(pos, lead int, limit bool) int {
if pos <= 0 {
}
}
up := 9
if limit {
up = a[pos]
}
ans := 0
for i := 0; i <= up; i++ {
if i == 0 && lead == 1 {
ans += dfs(pos-1, lead, limit && i == up)
} else if s[i] {
ans += dfs(pos-1, 0, limit && i == up)
}
}
if !limit {