Arrangement of the characters of a word such that all vowels are at odd places | Function to return the factorial of a number ; calculating nPr ; Function to find the number of $ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Get total even positions ; Get total odd positions ; Store $frequency of each character of the string ; Count total number of vowels ; Count total number of consonants ; Calculate the total number of ways ; Driver code
< ? php function fact ( $ n ) { $ f = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ f = $ f * $ i ; } return $ f ; } function npr ( $ n , $ r ) { return fact ( $ n ) / fact ( $ n - $ r ) ; } function countPermutations ( $ str ) { $ even = floor ( strlen ( $ str ) / 2 ) ; $ odd = strlen ( $ str ) - $ even ; $ ways = 0 ; $ freq = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { ++ $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ; } $ nvowels = $ freq [ 0 ] + $ freq [ 4 ] + $ freq [ 8 ] + $ freq [ 14 ] + $ freq [ 20 ] ; $ nconsonants = strlen ( $ str ) - $ nvowels ; $ ways = npr ( $ odd , $ nvowels ) * npr ( $ nconsonants , $ nconsonants ) ; return $ ways ; } $ str = " geeks " ; echo countPermutations ( $ str ) ; ? >