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 numberint 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 stringint permute(int n, int r){ int ans = 0; ans = (fact(n) / fact(n - r)); return ans;}// Function to find the total// number of combinations possibleint findPermutations(int n){ int sum = 0, P; for (int r = 1; r <= n; r++) { P = permute(n, r); sum = sum + P; } return sum;}// Driver Codeint main(){ string str = "xz"; int result, n; n = str.length(); cout << findPermutations(n); return 0;} |
Java
// Java implementation of the// above approachclass GFG{// Function to find the factorial// of a numberstatic 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 Stringstatic 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 possiblestatic 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 Codepublic 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 numberdef 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 stringdef permute(n, r): ans = 0 ans = fact(n) // fact(n - r) return ans# Function to find the total# number of combinations possibledef findPermutations(n): sum = 0 for r in range(1, n + 1): P = permute(n, r) sum = sum + P return sum# Driver Codestr = "xz"n = len(str)# Function callprint(findPermutations(n))# This code is contributed by Shivam Singh |
C#
// C# implementation of the// above approachusing System;class GFG{// Function to find the factorial// of a numberstatic 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 Stringstatic 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 possiblestatic 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 Codepublic 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 numberfunction 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 stringfunction permute(n, r){ var ans = 0; ans = (fact(n) / fact(n - r)); return ans;}// Function to find the total// number of combinations possiblefunction findPermutations(n){ var sum = 0, P; for (var r = 1; r <= n; r++) { P = permute(n, r); sum = sum + P; } return sum;}// Driver Codevar str = "xz";var result, n;n = str.length;document.write( findPermutations(n));</script> |
4
Time Complexity: O(n2)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



