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.
- Find total number of distinct years from a string
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Find distinct characters in distinct substrings of 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
- Print all permutation of a string using ArrayList
- String which when repeated exactly K times gives a permutation of S
- Find n-th lexicographically permutation of a string | Set 2
- Lexicographically smallest permutation of a string with given subsequences
- Maximum even length sub-string that is permutation of a palindrome
- Distinct permutations of the string | Set 2
- Check if given string can be split into four distinct strings
- Print all distinct permutations of a given string with duplicates
- Minimum changes to a string to make all substrings distinct
Improved By : parashar