Number of distinct permutation a String can have | Javascript program to find number of distinct permutations of a string . ; Utility function to find factorial of n . ; Returns count of distinct permutations of str . ; finding frequency of all the lower case alphabet and storing them in array of integer ; finding factorial of number of appearances and multiplying them since they are repeating alphabets ; finding factorial of size of string and dividing it by factorial found after multiplying ; Driver code
let MAX_CHAR = 26 ; function factorial ( n ) { let fact = 1 ; for ( let i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } function countDistinctPermutations ( str ) { let length = str . length ; let freq = new Array ( MAX_CHAR ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < length ; i ++ ) if ( str [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) ) freq [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let fact = 1 ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) fact = fact * factorial ( freq [ i ] ) ; return parseInt ( factorial ( length ) / fact , 10 ) ; } let str = " " ; document . write ( countDistinctPermutations ( str ) ) ;