In a program I am making that generates anagrams for a given set of letters, my current approach is to:
- Get all the the combinations of all the letters
- Get the permutations of each combination group
- Sort the resulting permutations alphabetically
- Remove duplicate entries
My question pertains to the mathematics of permutations. I am wondering if it is possible to flat-out calculate the array size needed to store all of the remaining entries after removal of duplicate entries (using, say, the number of repeated letters in conjunction with the permutation formula or something).
I apologize about the vagueness of my question, I am still researching more about combinations and permutations. I will try to elaborate my goal as my understanding of combinations and permutations expands, and once I re-familiarize myself with my program (it was a spare-time project of mine last summer).
If you have
nelements, anda[0]duplicates of one element,a[1]duplicates of another element, and so on up toa[k], then the total number of distinct permutations (up to duplicates) isn!/(a[0]! a[1]! ... a[k]!).FYI, if you’re interested, with Guava you could write
and the result would be the unique permutations of the characters, accounting for duplicates and everything. You could even call its
.size()method — or just look at its implementation for hints. (Disclosure: I contribute to Guava.)