Skip to content

Commit e3293ee

Browse files
committed
Time: 866 ms (93.65%) | Memory: 18.9 MB (98.41%) - LeetSync
1 parent 1487ecb commit e3293ee

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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

0 commit comments

Comments
 (0)