Skip to content

Commit 528ef74

Browse files
committedApr 12, 2025
Add solution #3272
1 parent 7624cb2 commit 528ef74

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,250 LeetCode solutions in JavaScript
1+
# 1,251 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1243,6 +1243,7 @@
12431243
3191|[Minimum Operations to Make Binary Array Elements Equal to One I](./solutions/3191-minimum-operations-to-make-binary-array-elements-equal-to-one-i.js)|Medium|
12441244
3208|[Alternating Groups II](./solutions/3208-alternating-groups-ii.js)|Medium|
12451245
3223|[Minimum Length of String After Operations](./solutions/3223-minimum-length-of-string-after-operations.js)|Medium|
1246+
3272|[Find the Count of Good Integers](./solutions/3272-find-the-count-of-good-integers.js)|Hard|
12461247
3306|[Count of Substrings Containing Every Vowel and K Consonants II](./solutions/3306-count-of-substrings-containing-every-vowel-and-k-consonants-ii.js)|Medium|
12471248
3356|[Zero Array Transformation II](./solutions/3356-zero-array-transformation-ii.js)|Medium|
12481249
3375|[Minimum Operations to Make Array Values Equal to K](./solutions/3375-minimum-operations-to-make-array-values-equal-to-k.js)|Easy|
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* 3272. Find the Count of Good Integers
3+
* https://leetcode.com/problems/find-the-count-of-good-integers/
4+
* Difficulty: Hard
5+
*
6+
* You are given two positive integers n and k.
7+
*
8+
* An integer x is called k-palindromic if:
9+
* - x is a palindrome.
10+
* - x is divisible by k.
11+
*
12+
* An integer is called good if its digits can be rearranged to form a k-palindromic integer.
13+
* For example, for k = 2, 2020 can be rearranged to form the k-palindromic integer 2002,
14+
* whereas 1010 cannot be rearranged to form a k-palindromic integer.
15+
*
16+
* Return the count of good integers containing n digits.
17+
*
18+
* Note that any integer must not have leading zeros, neither before nor after rearrangement.
19+
* For example, 1010 cannot be rearranged to form 101.
20+
*/
21+
22+
/**
23+
* @param {number} n
24+
* @param {number} k
25+
* @return {number}
26+
*/
27+
function countGoodIntegers(n, k) {
28+
if (n === 1) return Math.floor(9 / k);
29+
30+
let result = 0;
31+
const halfLength = Math.ceil(n / 2);
32+
const start = Math.pow(10, halfLength - 1);
33+
const end = Math.pow(10, halfLength);
34+
35+
const seen = new Set();
36+
for (let i = start; i < end; i++) {
37+
const palindromeStr = generatePalindrome(i);
38+
if (palindromeStr.length !== n) continue;
39+
const num = parseInt(palindromeStr);
40+
if (num % k !== 0) continue;
41+
42+
const sortedDigits = palindromeStr.split('').sort().join('');
43+
if (seen.has(sortedDigits)) continue;
44+
seen.add(sortedDigits);
45+
46+
result += calculateArrangements(palindromeStr);
47+
}
48+
49+
return result;
50+
51+
function generatePalindrome(firstHalf) {
52+
const str = firstHalf.toString();
53+
const isOdd = n % 2 === 1;
54+
return isOdd
55+
? str + str.slice(0, -1).split('').reverse().join('')
56+
: str + str.split('').reverse().join('');
57+
}
58+
59+
function calculateArrangements(numStr) {
60+
const freq = new Map();
61+
for (const digit of numStr) {
62+
freq.set(digit, (freq.get(digit) || 0) + 1);
63+
}
64+
const total = multinomial(n, freq);
65+
if (!freq.has('0')) return total;
66+
67+
freq.set('0', freq.get('0') - 1);
68+
if (freq.get('0') === 0) freq.delete('0');
69+
return total - multinomial(n - 1, freq);
70+
}
71+
72+
function multinomial(length, freq) {
73+
const numerator = factorial(length);
74+
let denominator = 1;
75+
for (const count of freq.values()) {
76+
denominator *= factorial(count);
77+
}
78+
return numerator / denominator;
79+
}
80+
81+
function factorial(num) {
82+
let res = 1;
83+
for (let i = 2; i <= num; i++) res *= i;
84+
return res;
85+
}
86+
}

0 commit comments

Comments
 (0)
Please sign in to comment.