-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3032-CountNumbersWithUniqueDigitsII.go
More file actions
93 lines (82 loc) · 2.48 KB
/
3032-CountNumbersWithUniqueDigitsII.go
File metadata and controls
93 lines (82 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
// 3032. Count Numbers With Unique Digits II
// Given two positive integers a and b,
// return the count of numbers having unique digits in the range [a, b] (inclusive).
// Example 1:
// Input: a = 1, b = 20
// Output: 19
// Explanation: All the numbers in the range [1, 20] have unique digits except 11. Hence, the answer is 19.
// Example 2:
// Input: a = 9, b = 19
// Output: 10
// Explanation: All the numbers in the range [9, 19] have unique digits except 11. Hence, the answer is 10.
// Example 3:
// Input: a = 80, b = 120
// Output: 27
// Explanation: There are 41 numbers in the range [80, 120], 27 of which have unique digits.
// Constraints:
// 1 <= a <= b <= 1000
import "fmt"
func numberCount(a int, b int) int {
res := 0
check := func(n int) bool {
set := make(map[int]bool)
for n > 0 {
d := n % 10;
if set[d] { return false }
set[d] = true
n /= 10
}
return true
}
for i := a; i <= b; i++ {
if check(i) { res++ }
}
return res
}
func numberCount1(a int, b int) int {
res := 0
check := func(n int) bool {
var h int
for i := n; i != 0; i /= 10 {
r := i % 10
if h &(1 << r) != 0 {
return false
}
h |= 1 << r
}
return true
}
for i := a; i <= b; i++ {
if check(i) {
res++
}
}
return res
}
func main() {
// Example 1:
// Input: a = 1, b = 20
// Output: 19
// Explanation: All the numbers in the range [1, 20] have unique digits except 11. Hence, the answer is 19.
fmt.Println(numberCount(1, 20)) // 19
// Example 2:
// Input: a = 9, b = 19
// Output: 10
// Explanation: All the numbers in the range [9, 19] have unique digits except 11. Hence, the answer is 10.
fmt.Println(numberCount(9, 19)) // 10
// Example 3:
// Input: a = 80, b = 120
// Output: 27
// Explanation: There are 41 numbers in the range [80, 120], 27 of which have unique digits.
fmt.Println(numberCount(80, 120)) // 27
fmt.Println(numberCount(1, 1)) // 1
fmt.Println(numberCount(1000, 1000)) // 0
fmt.Println(numberCount(1, 1000)) // 738
fmt.Println(numberCount1(1, 20)) // 19
fmt.Println(numberCount1(9, 19)) // 10
fmt.Println(numberCount1(80, 120)) // 27
fmt.Println(numberCount1(1, 1)) // 1
fmt.Println(numberCount1(1000, 1000)) // 0
fmt.Println(numberCount1(1, 1000)) // 738
}