This is a C++ program to find the number of permutations of a given string.

1. This algorithm prints a total number of permutation possible for a given string.

2. The time complexity of this algorithm is O(n).

1. This algorithm takes the input of the string.

2. Then it checks for the repetition of the characters.

3. Compute the permutation and print the result.

4. Exit.

C++ Program to find the number of permutations of a given string.

This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

#include<iostream> #include<string.h> using namespace std; // A function to find the factorial. int factorial(int n) { int i; for(i = n-1; i > 1; i--) n *= i; return n; } // A function to calculate the total number of permutation possible for the given set. int CountPermutation(char *str) { int countoccur[26] = {0}, len, i, res; len = strlen(str); // Count the occurrence of each character. for(i = 0; i < len; i++) { countoccur[str[i]-'a']++; } res = factorial(len); // Divide the length factorial by the factorial of number of occurrence of each character. for(i = 0; i < 26; i++) { if(countoccur[i] > 1) res = res/factorial(countoccur[i]); } return res; } int main() { int result; char str[100]; cout<<"A program to find a permutation of a given string: "; cout<<"\n\n\tEnter the string: "; cin>>str; // Get result using CountPermutation(). result = CountPermutaion(str); cout<<"\nThe number of possible permutation are: "<<result; return 0; }

1. Take the input of the string.

2. Call CountPermutation() with the string as an argument.

3. Inside CountPermutation, Firstly Count the number of occurrence of each character of the string.

4. Calculate the factorial of the length of the string and store in ‘res’.

5. Divide the ‘res’ by the factorial of a number of occurrence of each character if they count more than 1.

6. Return to main and print the result.

7. Exit.

Case 1: A program to find a permutation of a given string: Enter the string: aabcddd The number of possible permutation are: 420 Case 2: A program to find a permutation of a given string: Enter the string: vghhnhkkkvv The number of possible permutation are: 184800

**Sanfoundry Global Education & Learning Series – C++ Algorithms.**

To practice all C++ Algorithms, here is complete set of 1000 C++ Algorithms.