diff options
author | jack <0x6A73@pm.me> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <0x6A73@pm.me> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /count_special_integers.py | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'count_special_integers.py')
-rw-r--r-- | count_special_integers.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/count_special_integers.py b/count_special_integers.py new file mode 100644 index 0000000..b4a6d4d --- /dev/null +++ b/count_special_integers.py @@ -0,0 +1,35 @@ +# 2376. Count Special Integers +from math import perm + +""" +we call a positive integer special if all of its digits are distinct. given a +positive integer 'n', return the number of special integers that belong to +the interval '[1, n]' +""" + + +class Solution(object): + def countSpecialNumbers(self, n): + """ + :type n: int + :rtype: int + """ + l = list(map(int, str(n + 1))) + m = len(l) + ans = sum(9 * perm(9, i) for i in range(m - 1)) + s = set() + for i, x in enumerate(l): + for y in range(i == 0, x): + if y not in s: + ans += perm(9 - i, m - i - 1) + if x in s: + break + s.add(x) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.countSpecialNumbers(20)) + print(obj.countSpecialNumbers(5)) + print(obj.countSpecialNumbers(135)) |