938. Numbers At Most N Given Digit Set
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method Explanation
The problem asks us to count the number of positive integers that can be formed using a given set of digits, up to a certain integer n. The key idea is to solve this by breaking it down based on the number of digits in the generated numbers compared to n.
Solution
Numbers At Most N Given Digit Set in Python
class Solution:
def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:
S = str(n)
k = len(S)
d = len(digits)
# Part 1: Count numbers with fewer digits than n.
# For a length i (1 <= i < k), there are d^i possible numbers.
count = 0
for i in range(1, k):
count += d ** i
# Part 2: Count numbers with the same number of digits as n.
for i in range(k):
prefix_digit = S[i]
num_smaller_digits = 0
has_same_digit = False
# Iterate through the available digits to compare with n's current digit.
for digit in digits:
if digit < prefix_digit:
# This digit is smaller, so any number formed with this prefix will be smaller.
num_smaller_digits += 1
elif digit == prefix_digit:
# Found a digit that matches n's current digit. We can continue with this prefix.
has_same_digit = True
else: # digit > prefix_digit
break # Since digits is sorted, all subsequent digits will also be greater.
# For each smaller digit found, we can form d^(k-1-i) numbers for the remaining positions.
count += num_smaller_digits * (d ** (k - 1 - i))
# If n's current digit is not in our set, we can't form any more numbers
# with this prefix that are <= n. So we stop.
if not has_same_digit:
return count
# If we finished the loop, it means n itself can be formed.
return count + 1
Numbers At Most N Given Digit Set in Java
class Solution {
public int atMostNGivenDigitSet(String[] digits, int n) {
String s = String.valueOf(n);
int k = s.length();
int d = digits.length;
int count = 0;
// Part 1: Count numbers with fewer digits than n.
// For a length i (1 <= i < k), there are d^i possible numbers.
for (int i = 1; i < k; i++) {
count += Math.pow(d, i);
}
// Part 2: Count numbers with the same number of digits as n.
for (int i = 0; i < k; i++) {
char prefixChar = s.charAt(i);
int numSmallerDigits = 0;
boolean hasSameDigit = false;
// Iterate through the available digits to compare with n's current digit.
for (String digitStr : digits) {
if (digitStr.charAt(0) < prefixChar) {
// This digit is smaller, so any number formed with this prefix will be smaller.
numSmallerDigits++;
} else if (digitStr.charAt(0) == prefixChar) {
// Found a digit that matches n's current digit. We can continue with this prefix.
hasSameDigit = true;
break; // Since digits is sorted, no need to check further for smaller digits.
} else { // digitStr.charAt(0) > prefixChar
break; // All subsequent digits will also be greater.
}
}
// For each smaller digit found, we can form d^(k-1-i) numbers for the remaining positions.
count += numSmallerDigits * Math.pow(d, k - 1 - i);
// If n's current digit is not in our set, we can't form any more numbers
// with this prefix that are <= n. So we stop.
if (!hasSameDigit) {
return count;
}
}
// If we finished the loop, it means n itself can be formed.
return count + 1;
}
}
Numbers At Most N Given Digit Set in C++
class Solution {
public:
int atMostNGivenDigitSet(vector<string>& digits, int n) {
string s = to_string(n);
int k = s.length();
int d = digits.size();
int count = 0;
// Part 1: Count numbers with fewer digits than n.
// For a length i (1 <= i < k), there are d^i possible numbers.
for (int i = 1; i < k; ++i) {
count += pow(d, i);
}
// Part 2: Count numbers with the same number of digits as n.
for (int i = 0; i < k; ++i) {
char prefix_char = s[i];
int num_smaller_digits = 0;
bool has_same_digit = false;
// Iterate through the available digits to compare with n's current digit.
for (const string& digit_str : digits) {
if (digit_str[0] < prefix_char) {
// This digit is smaller, so any number formed with this prefix will be smaller.
num_smaller_digits++;
} else if (digit_str[0] == prefix_char) {
// Found a digit that matches n's current digit. We can continue with this prefix.
has_same_digit = true;
// Since digits is sorted, we can break early if we find an equal or greater.
} else { // digit_str[0] > prefix_char
break; // All subsequent digits will also be greater.
}
}
// For each smaller digit found, we can form d^(k-1-i) numbers for the remaining positions.
count += num_smaller_digits * pow(d, k - 1 - i);
// If n's current digit is not in our set, we can't form any more numbers
// with this prefix that are <= n. So we stop.
if (!has_same_digit) {
return count;
}
}
// If we finished the loop, it means n itself can be formed.
return count + 1;
}
};
Numbers At Most N Given Digit Set in TypeScript
function atMostNGivenDigitSet(digits: string[], n: number): number {
const s = String(n);
const k = s.length;
const d = digits.length;
let count = 0;
// Part 1: Count numbers with fewer digits than n.
// For a length i (1 <= i < k), there are d^i possible numbers.
for (let i = 1; i < k; i++) {
count += Math.pow(d, i);
}
// Part 2: Count numbers with the same number of digits as n.
for (let i = 0; i < k; i++) {
const prefixChar = s[i];
let numSmallerDigits = 0;
let hasSameDigit = false;
// Iterate through the available digits to compare with n's current digit.
for (const digit of digits) {
if (digit < prefixChar) {
// This digit is smaller, so any number formed with this prefix will be smaller.
numSmallerDigits++;
} else if (digit === prefixChar) {
// Found a digit that matches n's current digit. We can continue with this prefix.
hasSameDigit = true;
} else { // digit > prefixChar
break; // Since digits is sorted, all subsequent digits will also be greater.
}
}
// For each smaller digit found, we can form d^(k-1-i) numbers for the remaining positions.
count += numSmallerDigits * Math.pow(d, k - 1 - i);
// If n's current digit is not in our set, we can't form any more numbers
// with this prefix that are <= n. So we stop.
if (!hasSameDigit) {
return count;
}
}
// If we finished the loop, it means n itself can be formed.
return count + 1;
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.