357. Count Numbers with Unique Digits

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:

```Input: 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`
```

Constraints:

• `0 <= n <= 8`

357. Count Numbers with Unique Digits
``````struct Solution;

impl Solution {
fn count_numbers_with_unique_digits(n: i32) -> i32 {
if n < 11 {
Self::f(n)
} else {
Self::f(10)
}
}

fn f(n: i32) -> i32 {
match n {
1 => 10,
2 => 9 * 9 + 10,
3 => 9 * 9 * 8 + 9 * 9 + 10,
4 => 9 * 9 * 8 * 7 + 9 * 9 * 8 + 9 * 9 + 10,
5 => 9 * 9 * 8 * 7 * 6 + 9 * 9 * 8 * 7 + 9 * 9 * 8 + 9 * 9 + 10,
6 => 9 * 9 * 8 * 7 * 6 * 5 + 9 * 9 * 8 * 7 * 6 + 9 * 9 * 8 * 7 + 9 * 9 * 8 + 9 * 9 + 10,
7 => {
9 * 9 * 8 * 7 * 6 * 5 * 4
+ 9 * 9 * 8 * 7 * 6 * 5
+ 9 * 9 * 8 * 7 * 6
+ 9 * 9 * 8 * 7
+ 9 * 9 * 8
+ 9 * 9
+ 10
}
8 => {
9 * 9 * 8 * 7 * 6 * 5 * 4 * 3
+ 9 * 9 * 8 * 7 * 6 * 5 * 4
+ 9 * 9 * 8 * 7 * 6 * 5
+ 9 * 9 * 8 * 7 * 6
+ 9 * 9 * 8 * 7
+ 9 * 9 * 8
+ 9 * 9
+ 10
}
9 => {
9 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
+ 9 * 9 * 8 * 7 * 6 * 5 * 4 * 3
+ 9 * 9 * 8 * 7 * 6 * 5 * 4
+ 9 * 9 * 8 * 7 * 6 * 5
+ 9 * 9 * 8 * 7 * 6
+ 9 * 9 * 8 * 7
+ 9 * 9 * 8
+ 9 * 9
+ 10
}
10 => {
9 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
+ 9 * 9 * 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