Number of ways to arrange a word such that no vowels occur together | Function to check if a character is vowel or consonent ; Function to calculate factorial of a number ; Calculating no of ways for arranging vowels ; Iterate the map and count the number of vowels and calculate no of ways to arrange vowels ; Calculating no of ways to arrange the given word such that all vowels come together ; Calculate no of ways to arrange vowels ; To store denominator of fraction ; Count of consonents ; Calculate the number of ways to arrange the word such that all vowels come together ; To calculate total number of permutations ; To store length of the given word ; Denominator of fraction ; Return total number of permutations of the given word ; Function to calculate number of permutations such that no vowels come together ; To store frequency of character ; Count frequency of all characters ; Calculate total number of permutations ; Calculate total number of permutations such that all vowels come together ; Subtract vwl_tgthr from total to get the result ; Return the result ; Driver code
function isVowel ( ch ) { if ( ch == ' ' ch == ' ' ch == ' ' ch == ' ' ch == ' ' ) return true ; else return false ; } function fact ( n ) { if ( n < 2 ) { return 1 ; } return n * fact ( n - 1 ) ; } function only_vowels ( freq ) { let denom = 1 ; let cnt_vwl = 0 ; for ( let [ key , value ] of freq . entries ( ) ) { if ( isVowel ( key ) ) { denom *= fact ( value ) ; cnt_vwl += value ; } } return Math . floor ( fact ( cnt_vwl ) / denom ) ; } function all_vowels_together ( freq ) { let vow = only_vowels ( freq ) ; let denom = 1 ; let cnt_cnst = 0 ; for ( let [ key , value ] of freq . entries ( ) ) { if ( ! isVowel ( key ) ) { denom *= fact ( value ) ; cnt_cnst += value ; } } let ans = Math . floor ( fact ( cnt_cnst + 1 ) / denom ) ; return ( ans * vow ) ; } function total_permutations ( freq ) { let cnt = 0 ; let denom = 1 ; for ( let [ key , value ] of freq . entries ( ) ) { denom *= fact ( value ) ; cnt += value ; } return Math . floor ( fact ( cnt ) / denom ) ; } function no_vowels_together ( word ) { let freq = new Map ( ) ; for ( let i = 0 ; i < word . length ; i ++ ) { let ch = word [ i ] . toLowerCase ( ) ; if ( freq . has ( ch ) ) { freq . set ( ch , freq . get ( ch ) + 1 ) ; } else { freq . set ( ch , 1 ) ; } } let total = total_permutations ( freq ) ; let vwl_tgthr = all_vowels_together ( freq ) ; let res = total - vwl_tgthr ; return res ; } let word = " " ; let ans = no_vowels_together ( word ) ; document . write ( ans + " " ) ; word = " " ; ans = no_vowels_together ( word ) ; document . write ( ans + " " ) ; word = " " ; ans = no_vowels_together ( word ) ; document . write ( ans + " " ) ;