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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find total number of distinct years from a string
- Find the number of strings formed using distinct characters of a given 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
- Check if any permutation of string is a K times repeated string
- 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
- Print all permutation of a string using ArrayList
- 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
- Permutation of given string that maximizes count of Palindromic substrings
- Find the N-th lexicographic permutation of string using Factoradic method
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Distinct permutations of the string | Set 2
- Convert given string so that it holds only distinct characters
- Minimum changes to a string to make all substrings distinct
- Print all distinct permutations of a given string with duplicates
Improved By : parashar