Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10n.
Example 1:
Input: n = 2 Output: 91 Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100, excluding 11,22,33,44,55,66,77,88,99
Example 2:
Input: n = 0 Output: 1
Constraints:
0 <= n <= 8When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy is all about checking absolutely every number to see if it meets our unique digit requirement. We'll go through the numbers one by one, and for each one, we'll see if it has repeating digits.
Here's how the algorithm would work step-by-step:
def count_numbers_with_unique_digits_brute_force(number_of_digits):
if number_of_digits == 0:
return 1
count_of_unique_digit_numbers = 0
maximum_number = int('9' * number_of_digits)
for number in range(maximum_number + 1):
# Convert the number to a string to iterate through digits
number_as_string = str(number)
digits_seen = set()
is_unique = True
for digit_character in number_as_string:
digit = int(digit_character)
# If we've seen this digit before, it's not a unique number
if digit in digits_seen:
is_unique = False
break
digits_seen.add(digit)
# Only increment the count if all digits are unique
if is_unique:
count_of_unique_digit_numbers += 1
return count_of_unique_digit_numbersInstead of checking every possible number, we use a mathematical trick to count valid numbers based on the number of digits. This avoids unnecessary computations by cleverly calculating the possibilities.
Here's how the algorithm would work step-by-step:
def count_numbers_with_unique_digits(number_of_digits):
if number_of_digits == 0:
return 1
available_unique_numbers = 10
total_unique_numbers = 10
unique_digits_product = 9
# Account for the first digit not being zero.
for digit in range(2, min(number_of_digits + 1, 11)):
unique_digits_product = unique_digits_product * (available_unique_numbers - 1)
# Add the result to the total
total_unique_numbers += unique_digits_product
available_unique_numbers -= 1
return total_unique_numbers| Case | How to Handle |
|---|---|
| Input n = 0 | Return 1 since there is one number (0) with unique digits |
| Input n = 1 | Return 10 since there are 10 single-digit numbers (0-9) with unique digits |
| Input n > 10 | Since there are only 10 unique digits, any number with more than 10 digits must have duplicate digits, so cap n at 10. |
| Integer Overflow when calculating permutations | The maximum value of n is capped at 10, so integer overflow is not a concern with reasonably sized integer types. |
| Large value of n up to the imposed limit (n=10) | Iterative dynamic programming is more efficient than recursion as n scales up to its maximum size, avoiding stack overflow issues. |
| No valid solution exists other than the explicit constraints | The problem definition always ensures a solution by defining valid input n between 0 and 10 inclusive |
| Negative input for n | The constraints define valid input n between 0 and 10 inclusive so we should throw an exception or return 0 for negative inputs |
| Non-integer input for n | Cast the input to an integer to handle floating-point or other unexpected input types |