File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ )
6
+
7
+ func countNumbersWithUniqueDigits(n int) int {
8
+ // Base case: n = 0, only 1 possible number (0)
9
+ if n == 0 {
10
+ return 1
11
+ }
12
+
13
+ // For n > 0, initialize count to 10 (digits 0-9)
14
+ count := 10
15
+
16
+ // Calculate count for 2 to n digits
17
+ for i := 2; i <= n; i++ {
18
+ // For each number of digits, calculate the number of possible unique numbers
19
+ // First digit can be 1-9 (9 choices), subsequent digits can be any of the remaining 9-1 = 8 choices
20
+ // Multiply the choices together to get the total number of possible unique numbers
21
+ // Add this count to the total count
22
+ currCount := 9
23
+ for j := 1; j < i; j++ {
24
+ currCount *= 10 - j
25
+ }
26
+ count += currCount
27
+ }
28
+
29
+ return count
30
+ }
31
+
32
+ func main() {
33
+ // Test case with n = 2
34
+ fmt.Println(countNumbersWithUniqueDigits(2)) // Output: 91
35
+ }
You can’t perform that action at this time.
0 commit comments