Number of words that can be made using exactly P consonants and Q vowels from the given string | Function to return the value of nCk ; Function to return the factorial of n ; Function that returns true if ch is a vowel ; Function to return the number of words possible ; To store the count of vowels and consonanats in the given string ; If current character is a vowel ; Find the total possible words ; Driver code
function binomialCoeff ( n , k ) { if ( k == 0 k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; } function fact ( n ) { if ( n >= 1 ) return n * fact ( n - 1 ) ; else return 1 ; } function isVowel ( ch ) { if ( ch == ' ' ch == ' ' ch == ' ' ch == ' ' ch == ' ' ) { return true ; } return false ; } function countWords ( s , p , q ) { var countc = 0 , countv = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( isVowel ( s [ i ] ) ) countv ++ ; else countc ++ ; } var a = binomialCoeff ( countc , p ) ; var b = binomialCoeff ( countv , q ) ; var c = fact ( p + q ) ; var ans = ( a * b ) * c ; return ans ; } var s = " " ; var p = 4 , q = 3 ; document . write ( countWords ( s , p , q ) ) ;