File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
3548-find-the-count-of-good-integers Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countGoodIntegers (self , n : int , k : int ) -> int :
3
+ dictionary = set ()
4
+ base = 10 ** ((n - 1 ) // 2 )
5
+ skip = n & 1
6
+ # Enumerate the number of palindrome numbers of n digits
7
+ for i in range (base , base * 10 ):
8
+ s = str (i )
9
+ s += s [::- 1 ][skip :]
10
+ palindromicInteger = int (s )
11
+ # If the current palindrome number is a k-palindromic integer
12
+ if palindromicInteger % k == 0 :
13
+ sorted_s = "" .join (sorted (s ))
14
+ dictionary .add (sorted_s )
15
+
16
+ fac = [factorial (i ) for i in range (n + 1 )]
17
+ ans = 0
18
+ for s in dictionary :
19
+ cnt = [0 ] * 10
20
+ for c in s :
21
+ cnt [int (c )] += 1
22
+ # Calculate permutations and combinations
23
+ tot = (n - cnt [0 ]) * fac [n - 1 ]
24
+ for x in cnt :
25
+ tot //= fac [x ]
26
+ ans += tot
27
+
28
+ return ans
You can’t perform that action at this time.
0 commit comments