Skip to content
Related Articles

Related Articles

Count of distinct permutations of every possible length of given string
  • Difficulty Level : Basic
  • Last Updated : 25 Aug, 2020

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 
Output: 
4

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :