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 109 + 7.
A permutation is a rearrangement of all the characters of a string.
Example 1:
Input: num = "123"
Output: 2
Explanation:
num are "123", "132", "213", "231", "312" and "321"."132" and "231" are balanced. Thus, the answer is 2.Example 2:
Input: num = "112"
Output: 1
Explanation:
num are "112", "121", and "211"."121" is balanced. Thus, the answer is 1.Example 3:
Input: num = "12345"
Output: 0
Explanation:
num are balanced, so the answer is 0.
Constraints:
2 <= num.length <= 80num consists of digits '0' to '9' only.This approach involves generating all distinct permutations of the string and then checking each permutation to determine if it is balanced. For a string of length n, generate all permutations and check if the sum of digits at even indices equals the sum of digits at odd indices.
The code uses Python's itertools.permutations to generate all unique permutations of the input string. For each permutation, it calculates sums of digits at even and odd indices and checks if they are equal. If they are, it counts the permutation as balanced. The final count is returned modulo 10^9 + 7.
C
C++
Java
C#
JavaScript
Time Complexity: O(n! * n), where n is the length of the string. Generating permutations takes O(n!) and checking each permutation takes O(n).
Space Complexity: O(n!) due to the storage of all permutations in the set.
The optimized approach involves using backtracking to selectively generate permutations. The algorithm uses a recursive approach for swapping based on the feasibility of forming a balanced sum. It avoids generating all permutations explicitly, thereby reducing the complexity significantly.
The implementation involves backtracking with position tracking. It recursively estimates whether adding a digit at an even or odd position can lead to the required balance by adjusting sums dynamically. A frequency counter is used to ensure digits aren't reused.
C
C++
Java
C#
JavaScript
Time Complexity: O(n!), improved by pruning states early.
Space Complexity: O(n) due to recursion stack usage.
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n! * n), where n is the length of the string. Generating permutations takes O(n!) and checking each permutation takes O(n). Space Complexity: O(n!) due to the storage of all permutations in the set. |
| Optimized Pre-computation Approach Using Backtracking | Time Complexity: O(n!), improved by pruning states early. Space Complexity: O(n) due to recursion stack usage. |
Backtracking: Permutations - Leetcode 46 - Python • NeetCode • 390,029 views views
Watch 9 more video solutions →Practice Count Number of Balanced Permutations with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor