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
function fact ( n ) { let f = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { f = f * i ; } return f ; } function npr ( n , r ) { return fact ( n ) / fact ( n - r ) ; } function countPermutations ( str ) { let even = Math . floor ( str . length / 2 ) ; let odd = str . length - even ; let ways = 0 ; let freq = new Array ( 26 ) . fill ( 0 ) ; for ( let i = 0 ; i < str . length ; i ++ ) { ++ freq [ str . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ; } let nvowels = freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ; let nconsonants = str . length - nvowels ; ways = npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ; return ways ; } let str = " " ; document . write ( countPermutations ( str ) ) ;