We are given a string having only lowercase alphabets. The task is to find out total number of distinct permutation can be generated by that string.
Input : aab Output : 3 Different permutations are "aab", "aba" and "baa". Input : ybghjhbuytb Output : 1663200
A simple solution is to find all the distinct permutation and count them.
We can find the count without finding all permutation. Idea is to find all the characters that is getting repeated, i.e., frequency of all the character. Then, we divide the factorial of the length of string by multiplication of factorial of frequency of characters.
In second example, number of character is 11 and here h and y are repeated 2 times whereas g is repeated 3 times.
So, number of permutation is 11! / (2!2!3!) = 1663200
Below is the implementation of above idea.
This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Find total number of distinct years from a string
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Minimum length of string having all permutation of given string.
- Lexicographically n-th permutation of a string
- String which when repeated exactly K times gives a permutation of S
- Find n-th lexicographically permutation of a string | Set 2
- Maximum even length sub-string that is permutation of a palindrome
- Lexicographically smallest permutation of a string with given subsequences
- Distinct permutations of the string | Set 2
- Print all distinct permutations of a given string with duplicates
- Find all distinct palindromic sub-strings of a given string
- Minimum changes to a string to make all substrings distinct
- Check if given string can be split into four distinct strings
- Check whether count of distinct characters in a string is Prime or not
Improved By : parashar