diff options
author | Jack Sangdahl <[email protected]> | 2025-05-08 23:54:32 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-05-08 23:54:32 -0600 |
commit | 67d6c0ec318b5e9726ce680cb17df94e55510ba2 (patch) | |
tree | 444484bf0b7171a8cec399587eb58466eed72efd | |
parent | 9b8f993a101cb3f7ab3fdb753c3956a6e9a256c2 (diff) | |
download | leetcode-67d6c0ec318b5e9726ce680cb17df94e55510ba2.tar.gz leetcode-67d6c0ec318b5e9726ce680cb17df94e55510ba2.zip |
3343. count number of balanced permutations
-rw-r--r-- | count_num_balanced_perm.c | 78 | ||||
-rw-r--r-- | count_num_balanced_perm.py | 55 |
2 files changed, 133 insertions, 0 deletions
diff --git a/count_num_balanced_perm.c b/count_num_balanced_perm.c new file mode 100644 index 0000000..203f8c2 --- /dev/null +++ b/count_num_balanced_perm.c @@ -0,0 +1,78 @@ +// 3343. Count Number of Balanced Permutations +#include "leetcode.h" + +/* + * you are given a string 'num'. a string of digits is called balanced if the + * sum of the digits at even indices is equal to the sum of the digits at odd + * indices. return the number of distinct permutations of 'num' that are + * balanced. since the answer may be very large, return it modulo 10e9+7. a + * permutation is a rearrangement of all the characters of a string. + */ + +static int mod = 1e9 + 7; + +long long mod_exp(long long base, long long exp) { + long long res = 1; + base %= mod; + while (exp) { + if (exp & 1) + res = (res * base) % mod; + base = (base * base) % mod; + exp >>= 1; + } + return res; +} + +int countBalancedPermutations(char *num) { + int n = strlen(num), freq[10] = {0}; + for (int i = 0; i < n; i++) + freq[num[i] - '0']++; + int even_cnt = (n + 1) / 2, odd_cnt = n - even_cnt, sum = 0; + for (int i = 0; i < 10; i++) + sum += i * freq[i]; + if (sum & 1) + return 0; + int target = sum / 2, max = n; + long long *fac = (long long *)malloc((max + 1) * sizeof(long long)); + long long *ifac = (long long *)malloc((max + 1) * sizeof(long long)); + fac[0] = 1; + for (int i = 1; i <= max; i++) + fac[i] = (fac[i - 1] * i) % mod; + ifac[max] = mod_exp(fac[max], mod - 2); + for (int i = max - 1; i >= 0; i--) + ifac[i] = (ifac[i + 1] * (i + 1)) % mod; + long long dp[11][42][730] = {{{0}}}; + dp[0][0][0] = 1; + for (int i = 0; i < 10; i++) { + for (int j = 0; j <= even_cnt; j++) { + for (int k = 0; k <= target; k++) { + if (!dp[i][j][k]) + continue; + int curr = freq[i]; + for (int l = 0; l <= curr; l++) { + if (j + l > even_cnt) + break; + int ns = k + i * l; + if (ns > target) + break; + long long coef = (ifac[l] * ifac[curr - l]) % mod; + dp[i + 1][j + l][ns] = + (dp[i + 1][j + l][ns] + dp[i][j][k] * coef) % mod; + } + } + } + } + long long ans = dp[10][even_cnt][target]; + ans = (ans * fac[even_cnt]) % mod; + ans = (ans * fac[odd_cnt]) % mod; + free(fac); + free(ifac); + return (int)ans; +} + +int main() { + char *n1 = "123", *n2 = "112", *n3 = "12345"; + printf("%d\n", countBalancedPermutations(n1)); // expect: 2 + printf("%d\n", countBalancedPermutations(n2)); // expect: 1 + printf("%d\n", countBalancedPermutations(n3)); // expect: 0 +} diff --git a/count_num_balanced_perm.py b/count_num_balanced_perm.py new file mode 100644 index 0000000..eebf3d7 --- /dev/null +++ b/count_num_balanced_perm.py @@ -0,0 +1,55 @@ +# 3343. Count Number of Balanced Permutations +from collections import Counter +from math import comb + +""" +you are given a string 'num'. a string of digits is called balanced if the +sum of the digits at even indices is equal to the sum of the digits at odd +indices. return the number of distinct permutations of 'num' that are +balanced. since the answer may be very large, return it modulo 10e9+7. a +permutation is a rearrangement of all the characters of a string. +""" + + +class Solution(object): + def countBalancedPermutations(self, num): + """ + :type num: str + :rtype: int + """ + mod, n = 10**9 + 7, len(num) + total = sum(int(i) for i in num) + if total % 2: + return 0 + fac = [1] * (n + 1) + ifac = [1] * (n + 1) + inv = [1] * (n + 1) + for i in range(1, n + 1): + fac[i] = fac[i - 1] * i % mod + for i in range(2, n + 1): + inv[i] = mod - (mod // i) * inv[mod % i] % mod + for i in range(1, n + 1): + ifac[i] = ifac[i - 1] * inv[i] % mod + half_sum = total // 2 + half_len = n // 2 + dp = [[0] * (half_len + 1) for _ in range(half_sum + 1)] + dp[0][0] = 1 + digits = [0] * 10 + for c in num: + d = int(c) + digits[d] += 1 + for i in range(half_sum, d - 1, -1): + for j in range(half_len, 0, -1): + dp[i][j] = (dp[i][j] + dp[i - d][j - 1]) % mod + ans = dp[half_sum][half_len] + ans = ans * fac[half_len] % mod * fac[n - half_len] % mod + for cnt in digits: + ans = ans * ifac[cnt] % mod + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.countBalancedPermutations(num="123")) + print(obj.countBalancedPermutations(num="112")) + print(obj.countBalancedPermutations(num="12345")) |