Permutations of string such that no two vowels are adjacent | Factorial of a number ; Function to find c ( n , r ) ; Function to count permutations of string such that no two vowels are adjacent ; Finding the frequencies of the characters ; finding the no . of vowels and consonants in given word ; finding places for the vowels ; ways to fill consonants 6 ! / 2 ! ; ways to put vowels 7 C5 x 5 ! ; Driver code
def factorial ( n ) : NEW_LINE INDENT fact = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE DEDENT return fact NEW_LINE DEDENT def ncr ( n , r ) : NEW_LINE INDENT return factorial ( n ) // ( factorial ( r ) * factorial ( n - r ) ) NEW_LINE DEDENT def countWays ( string ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE nvowels , nconsonants = 0 , 0 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( i == 0 or i == 4 or i == 8 or i == 14 or i == 20 ) : NEW_LINE INDENT nvowels += freq [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT nconsonants += freq [ i ] NEW_LINE DEDENT DEDENT vplaces = nconsonants + 1 NEW_LINE cways = factorial ( nconsonants ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( i != 0 and i != 4 and i != 8 and i != 14 and i != 20 and freq [ i ] > 1 ) : NEW_LINE INDENT cways = cways // factorial ( freq [ i ] ) NEW_LINE DEDENT DEDENT vways = ncr ( vplaces , nvowels ) * factorial ( nvowels ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( i == 0 or i == 4 or i == 8 or i == 14 or i == 20 and freq [ i ] > 1 ) : NEW_LINE INDENT vways = vways // factorial ( freq [ i ] ) NEW_LINE DEDENT DEDENT return cways * vways ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " permutation " NEW_LINE print ( countWays ( string ) ) NEW_LINE DEDENT