Number of ways to arrange a word such that all vowels occur together | Factorial of a number ; Calculating ways for arranging consonants ; Ignore vowels ; Calculating ways for arranging vowels ; Function to count total no . of ways ; Count vowels and consonant ; Total no . of ways ; Driver code
function fact ( n ) { let f = 1 ; for ( let i = 2 ; i <= n ; i ++ ) f = f * i ; return f ; } function waysOfConsonants ( size1 , freq ) { let ans = fact ( size1 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 ) continue ; else ans = Math . floor ( ans / fact ( freq [ i ] ) ) ; } return ans ; } function waysOfVowels ( size2 , freq ) { return Math . floor ( fact ( size2 ) / ( fact ( freq [ 0 ] ) * fact ( freq [ 4 ] ) * fact ( freq [ 8 ] ) * fact ( freq [ 14 ] ) * fact ( freq [ 20 ] ) ) ) ; } function countWays ( str ) { let freq = new Array ( 200 ) ; for ( let i = 0 ; i < 200 ; i ++ ) freq [ i ] = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; let vowel = 0 , consonant = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] != ' ' && str [ i ] != ' ' && str [ i ] != ' ' && str [ i ] != ' ' && str [ i ] != ' ' ) consonant ++ ; else vowel ++ ; } return waysOfConsonants ( consonant + 1 , freq ) * waysOfVowels ( vowel , freq ) ; } let str = " " ; document . write ( countWays ( str ) ) ;