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
def binomialCoeff ( n , k ) : NEW_LINE INDENT if ( k == 0 or k == n ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) NEW_LINE DEDENT def fact ( n ) : NEW_LINE INDENT if ( n >= 1 ) : NEW_LINE INDENT return n * fact ( n - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT def isVowel ( ch ) : NEW_LINE INDENT if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def countWords ( s , p , q ) : NEW_LINE INDENT countc = 0 NEW_LINE countv = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) ) : NEW_LINE INDENT countv += 1 NEW_LINE DEDENT else : NEW_LINE INDENT countc += 1 NEW_LINE DEDENT DEDENT a = binomialCoeff ( countc , p ) NEW_LINE b = binomialCoeff ( countv , q ) NEW_LINE c = fact ( p + q ) NEW_LINE ans = ( a * b ) * c NEW_LINE return ans NEW_LINE DEDENT s = " crackathon " NEW_LINE p = 4 NEW_LINE q = 3 NEW_LINE print ( countWords ( s , p , q ) ) NEW_LINE