Count of distinct permutations of every possible length of given string
Given a string S, the task is to count the distinct permutations of every possible length of the given string.
Note: Repetition of characters is not allowed in the string.
Input: S = “abc”
Output: 15
Explanation:
Possible Permutations of every length are:
{“a”, “b”, “c”, “ab”, “bc”, “ac”, “ba”, “ca”, “cb”, “abc”, “acb”, “bac”, “bca”, “cab”, “cba”}Input: S = “xz”
Output: 4
Approach: The idea is to find the count of combinations of every possible length of the string and their sum is the total number of distinct permutations possible of different lengths. Therefore, for N length string total number of distinct permutation of different length is:
Total Combinations possible: nP1 + nP2 + nP3 + nP4 + …… + nPn
Below is the implementation of the above approach:
C++
// C++ implementation of the // above approach #include <bits/stdc++.h> #include <iostream> using namespace std; // Function to find the factorial // of a number int fact(int a) { int i, f = 1; // Loop to find the factorial // of the given number for (i = 2; i <= a; i++) f = f * i; return f; } // Function to find the number // of permutations possible // for a given string int permute(int n, int r) { int ans = 0; ans = (fact(n) / fact(n - r)); return ans; } // Function to find the total // number of combinations possible int findPermutations(int n) { int sum = 0, P; for (int r = 1; r <= n; r++) { P = permute(n, r); sum = sum + P; } return sum; } // Driver Code int main() { string str = "xz"; int result, n; n = str.length(); cout << findPermutations(n); return 0; }
Java
// Java implementation of the // above approach class GFG{ // Function to find the factorial // of a number static int fact(int a) { int i, f = 1; // Loop to find the factorial // of the given number for(i = 2; i <= a; i++) f = f * i; return f; } // Function to find the number // of permutations possible // for a given String static int permute(int n, int r) { int ans = 0; ans = (fact(n) / fact(n - r)); return ans; } // Function to find the total // number of combinations possible static int findPermutations(int n) { int sum = 0, P; for(int r = 1; r <= n; r++) { P = permute(n, r); sum = sum + P; } return sum; } // Driver Code public static void main(String[] args) { String str = "xz"; int result, n; n = str.length(); System.out.print(findPermutations(n)); } } // This code is contributed by Amit Katiyar
Python3
# Python3 program to implement # the above approach # Function to find the factorial # of a number def fact(a): f = 1 # Loop to find the factorial # of the given number for i in range(2, a + 1): f = f * i return f # Function to find the number # of permutations possible # for a given string def permute(n, r): ans = 0 ans = fact(n) // fact(n - r) return ans # Function to find the total # number of combinations possible def findPermutations(n): sum = 0 for r in range(1, n + 1): P = permute(n, r) sum = sum + P return sum # Driver Code str = "xz" n = len(str) # Function call print(findPermutations(n)) # This code is contributed by Shivam Singh
C#
// C# implementation of the // above approach using System; class GFG{ // Function to find the factorial // of a number static int fact(int a) { int i, f = 1; // Loop to find the factorial // of the given number for(i = 2; i <= a; i++) f = f * i; return f; } // Function to find the number // of permutations possible // for a given String static int permute(int n, int r) { int ans = 0; ans = (fact(n) / fact(n - r)); return ans; } // Function to find the total // number of combinations possible static int findPermutations(int n) { int sum = 0, P; for(int r = 1; r <= n; r++) { P = permute(n, r); sum = sum + P; } return sum; } // Driver Code public static void Main(String[] args) { String str = "xz"; int n; n = str.Length; Console.Write(findPermutations(n)); } } // This code is contributed by amal kumar choubey
Javascript
<script> // Javascript implementation of the // above approach // Function to find the factorial // of a number function fact(a) { var i, f = 1; // Loop to find the factorial // of the given number for (i = 2; i <= a; i++) f = f * i; return f; } // Function to find the number // of permutations possible // for a given string function permute(n, r) { var ans = 0; ans = (fact(n) / fact(n - r)); return ans; } // Function to find the total // number of combinations possible function findPermutations(n) { var sum = 0, P; for (var r = 1; r <= n; r++) { P = permute(n, r); sum = sum + P; } return sum; } // Driver Code var str = "xz"; var result, n; n = str.length; document.write( findPermutations(n)); </script>
4
Time Complexity: O(n2)
Auxiliary Space: O(1)
Please Login to comment...