Problem
Given an integern, return the count of all numbers with unique digits, x, where 0 <= x < 10^n.
Examples
Constraints
- 0 <= n <= 8
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 357 with 12 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 357 # by problem number
lcpy gen -s count_numbers_with_unique_digits # by problem name
n, return the count of all numbers with unique digits, x, where 0 <= x < 10^n.
Input: n = 2
Output: 91
Input: n = 0
Output: 1
class Solution:
# Time: O(n)
# Space: O(1)
def count_numbers_with_unique_digits(self, n: int) -> int:
if n == 0:
return 1
total = 10
count = 9
available = 9
for _ in range(2, n + 1):
count *= available
available -= 1
total += count
return total
| Time | Space |
|---|---|
| O(n) | O(1) |