Number of permutations of a string in which all the occurrences of a given character occurs together | Function to return factorial of the number passed as argument ; Function to get the total permutations which satisfy the given condition ; Create has to store count of each character ; Store character occurrences ; Count number of times Particular character comes ; If particular character isn 't  present in the string then return 0 ; Remove count of particular character ; Total length of the string ; Assume all occurrences of particular character as a single character . ; Compute factorial of the length ; Divide by the factorials of the no . of occurrences of all the characters . ; return the result ; Driver Code ; Assuming the string and the character are all in uppercase
function fact ( n ) { let result = 1 ; for ( let i = 1 ; i <= n ; i ++ ) result *= i ; return result ; } function getResult ( str , ch ) { let has = new Array ( 26 ) . fill ( null ) ; for ( let i = 0 ; i < str . length ; i ++ ) has [ str . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ++ ; particular = has [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; if ( particular == 0 ) return 0 ; has [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 0 ; let total = str . length ; total = total - particular + 1 ; let result = fact ( total ) ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( has [ i ] > 1 ) { result = result / fact ( has [ i ] ) ; } } return result ; } let str = " " ; document . write ( getResult ( str , ' ' ) + " " ) ;