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++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int fact( int a)
{
int i, f = 1;
for (i = 2; i <= a; i++)
f = f * i;
return f;
}
int permute( int n, int r)
{
int ans = 0;
ans = (fact(n) / fact(n - r));
return ans;
}
int findPermutations( int n)
{
int sum = 0, P;
for ( int r = 1; r <= n; r++) {
P = permute(n, r);
sum = sum + P;
}
return sum;
}
int main()
{
string str = "xz" ;
int result, n;
n = str.length();
cout << findPermutations(n);
return 0;
}
|
Java
class GFG{
static int fact( int a)
{
int i, f = 1 ;
for (i = 2 ; i <= a; i++)
f = f * i;
return f;
}
static int permute( int n, int r)
{
int ans = 0 ;
ans = (fact(n) / fact(n - r));
return ans;
}
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;
}
public static void main(String[] args)
{
String str = "xz" ;
int result, n;
n = str.length();
System.out.print(findPermutations(n));
}
}
|
Python3
def fact(a):
f = 1
for i in range ( 2 , a + 1 ):
f = f * i
return f
def permute(n, r):
ans = 0
ans = fact(n) / / fact(n - r)
return ans
def findPermutations(n):
sum = 0
for r in range ( 1 , n + 1 ):
P = permute(n, r)
sum = sum + P
return sum
str = "xz"
n = len ( str )
print (findPermutations(n))
|
C#
using System;
class GFG{
static int fact( int a)
{
int i, f = 1;
for (i = 2; i <= a; i++)
f = f * i;
return f;
}
static int permute( int n, int r)
{
int ans = 0;
ans = (fact(n) / fact(n - r));
return ans;
}
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;
}
public static void Main(String[] args)
{
String str = "xz" ;
int n;
n = str.Length;
Console.Write(findPermutations(n));
}
}
|
Javascript
<script>
function fact(a)
{
var i, f = 1;
for (i = 2; i <= a; i++)
f = f * i;
return f;
}
function permute(n, r)
{
var ans = 0;
ans = (fact(n) / fact(n - r));
return ans;
}
function findPermutations(n)
{
var sum = 0, P;
for ( var r = 1; r <= n; r++) {
P = permute(n, r);
sum = sum + P;
}
return sum;
}
var str = "xz" ;
var result, n;
n = str.length;
document.write( findPermutations(n));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Last Updated :
12 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...