|
| 1 | +package g3201_3300.s3272_find_the_count_of_good_integers; |
| 2 | + |
| 3 | +// #Hard #Hash_Table #Math #Enumeration #Combinatorics |
| 4 | +// #2024_09_02_Time_167_ms_(100.00%)_Space_54.5_MB_(100.00%) |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +public class Solution { |
| 15 | + private final List<String> palindromes = new ArrayList<>(); |
| 16 | + |
| 17 | + private long factorial(int n) { |
| 18 | + long res = 1; |
| 19 | + for (int i = 2; i <= n; i++) { |
| 20 | + res *= i; |
| 21 | + } |
| 22 | + return res; |
| 23 | + } |
| 24 | + |
| 25 | + private Map<Character, Integer> countDigits(String s) { |
| 26 | + Map<Character, Integer> freq = new HashMap<>(); |
| 27 | + for (char c : s.toCharArray()) { |
| 28 | + freq.put(c, freq.getOrDefault(c, 0) + 1); |
| 29 | + } |
| 30 | + return freq; |
| 31 | + } |
| 32 | + |
| 33 | + private long calculatePermutations(Map<Character, Integer> freq, int length) { |
| 34 | + long totalPermutations = factorial(length); |
| 35 | + for (int count : freq.values()) { |
| 36 | + totalPermutations /= factorial(count); |
| 37 | + } |
| 38 | + return totalPermutations; |
| 39 | + } |
| 40 | + |
| 41 | + private long calculateValidPermutations(String s) { |
| 42 | + Map<Character, Integer> freq = countDigits(s); |
| 43 | + int n = s.length(); |
| 44 | + long totalPermutations = calculatePermutations(freq, n); |
| 45 | + if (freq.getOrDefault('0', 0) > 0) { |
| 46 | + freq.put('0', freq.get('0') - 1); |
| 47 | + long invalidPermutations = calculatePermutations(freq, n - 1); |
| 48 | + totalPermutations -= invalidPermutations; |
| 49 | + } |
| 50 | + return totalPermutations; |
| 51 | + } |
| 52 | + |
| 53 | + private void generatePalindromes( |
| 54 | + int f, int r, int k, int lb, int sum, StringBuilder ans, int[] rem) { |
| 55 | + if (f > r) { |
| 56 | + if (sum == 0) { |
| 57 | + palindromes.add(ans.toString()); |
| 58 | + } |
| 59 | + return; |
| 60 | + } |
| 61 | + for (int i = lb; i <= 9; i++) { |
| 62 | + ans.setCharAt(f, (char) ('0' + i)); |
| 63 | + ans.setCharAt(r, (char) ('0' + i)); |
| 64 | + int chk = sum; |
| 65 | + chk = (chk + rem[f] * i) % k; |
| 66 | + if (f != r) { |
| 67 | + chk = (chk + rem[r] * i) % k; |
| 68 | + } |
| 69 | + generatePalindromes(f + 1, r - 1, k, 0, chk, ans, rem); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private List<String> allKPalindromes(int n, int k) { |
| 74 | + StringBuilder ans = new StringBuilder(n); |
| 75 | + ans.append("0".repeat(Math.max(0, n))); |
| 76 | + int[] rem = new int[n]; |
| 77 | + rem[0] = 1; |
| 78 | + for (int i = 1; i < n; i++) { |
| 79 | + rem[i] = (rem[i - 1] * 10) % k; |
| 80 | + } |
| 81 | + palindromes.clear(); |
| 82 | + generatePalindromes(0, n - 1, k, 1, 0, ans, rem); |
| 83 | + return palindromes; |
| 84 | + } |
| 85 | + |
| 86 | + public long countGoodIntegers(int n, int k) { |
| 87 | + List<String> ans = allKPalindromes(n, k); |
| 88 | + Set<String> st = new HashSet<>(); |
| 89 | + for (String str : ans) { |
| 90 | + char[] arr = str.toCharArray(); |
| 91 | + Arrays.sort(arr); |
| 92 | + st.add(new String(arr)); |
| 93 | + } |
| 94 | + List<String> v = new ArrayList<>(st); |
| 95 | + long chk = 0; |
| 96 | + for (String str : v) { |
| 97 | + long cc = calculateValidPermutations(str); |
| 98 | + chk += cc; |
| 99 | + } |
| 100 | + return chk; |
| 101 | + } |
| 102 | +} |
0 commit comments