|
| 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