[LeetCode] Count Numbers With Unique Digits II

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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
bool ok(int x) {
int bit = 0;
while(x) {
int k = x % 10;
if(bit & 1ll<<k) return false;
bit ^= (1ll<<k);
x /= 10;
}
return true;
}
public:
int numberCount(int a, int b) {
int res = 0;
for(int i = a; i <= b; i++) if(ok(i)) res++;
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/14/PS/LeetCode/count-numbers-with-unique-digits-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.