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
< ? php function factorial ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ fact = $ fact * $ i ; return $ fact ; } function ncr ( $ n , $ r ) { return factorial ( $ n ) / ( factorial ( $ r ) * factorial ( $ n - $ r ) ) ; } function countWays ( $ str ) { $ freq = array_fill ( 0 , 26 , NULL ) ; $ nvowels = 0 ; $ nconsonants = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) ++ $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ i == 0 $ i == 4 $ i == 8 $ i == 14 $ i == 20 ) $ nvowels += $ freq [ $ i ] ; else $ nconsonants += $ freq [ $ i ] ; } $ vplaces = $ nconsonants + 1 ; $ cways = factorial ( $ nconsonants ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ i != 0 && $ i != 4 && $ i != 8 && $ i != 14 && $ i != 20 && $ freq [ $ i ] > 1 ) { $ cways = $ cways / factorial ( $ freq [ $ i ] ) ; } } $ vways = ncr ( $ vplaces , $ nvowels ) * factorial ( $ nvowels ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ i == 0 $ i == 4 $ i == 8 $ i == 14 $ i == 20 && $ freq [ $ i ] > 1 ) { $ vways = $ vways / factorial ( $ freq [ $ i ] ) ; } } return $ cways * $ vways ; } $ str = " permutation " ; echo countWays ( $ str ) . " STRNEWLINE " ; return 0 ; ? >