Skip to content

Commit da4044d

Browse files
committed
Sync LeetCode submission Runtime - 853 ms (98.41%), Memory - 18.9 MB (98.41%)
1 parent 7839532 commit da4044d

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
2+
3+
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
4+
5+
<ul>
6+
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
7+
<li><code>x</code> is divisible by <code>k</code>.</li>
8+
</ul>
9+
10+
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
11+
12+
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
13+
14+
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<div class="example-block">
20+
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
21+
22+
<p><strong>Output:</strong> <span class="example-io">27</span></p>
23+
24+
<p><strong>Explanation:</strong></p>
25+
26+
<p><em>Some</em> of the good integers are:</p>
27+
28+
<ul>
29+
<li>551 because it can be rearranged to form 515.</li>
30+
<li>525 because it is already k-palindromic.</li>
31+
</ul>
32+
</div>
33+
34+
<p><strong class="example">Example 2:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
38+
39+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
40+
41+
<p><strong>Explanation:</strong></p>
42+
43+
<p>The two good integers are 4 and 8.</p>
44+
</div>
45+
46+
<p><strong class="example">Example 3:</strong></p>
47+
48+
<div class="example-block">
49+
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
50+
51+
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
52+
</div>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Constraints:</strong></p>
56+
57+
<ul>
58+
<li><code>1 &lt;= n &lt;= 10</code></li>
59+
<li><code>1 &lt;= k &lt;= 9</code></li>
60+
</ul>
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)