Number of ways to arrange a word such that no vowels occur together | Java code for above approach ; Function to check if a character is vowel or consonent ; Function to calculate factorial of a number ; Calculating no of ways for arranging vowels ; Iterate the map and count the number of vowels and calculate no of ways to arrange vowels ; Calculating no of ways to arrange the given word such that all vowels come together ; Calculate no of ways to arrange vowels ; To store denominator of fraction ; Count of consonents ; Calculate the number of ways to arrange the word such that all vowels come together ; To calculate total number of permutations ; To store length of the given word ; Denominator of fraction ; Return total number of permutations of the given word ; Function to calculate number of permutations such that no vowels come together ; To store frequency of character ; Count frequency of all characters ; Calculate total number of permutations ; Calculate total number of permutations such that all vowels come together ; Subtract vwl_tgthr from total to get the result ; Return the result ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; else return false ; } static long fact ( long n ) { if ( n < 2 ) { return 1 ; } return n * fact ( n - 1 ) ; } static long only_vowels ( HashMap < Character , Integer > freq ) { long denom = 1 ; long cnt_vwl = 0 ; for ( Map . Entry < Character , Integer > itr : freq . entrySet ( ) ) { if ( isVowel ( itr . getKey ( ) ) ) { denom *= fact ( itr . getValue ( ) ) ; cnt_vwl += itr . getValue ( ) ; } } return fact ( cnt_vwl ) / denom ; } static long all_vowels_together ( HashMap < Character , Integer > freq ) { long vow = only_vowels ( freq ) ; long denom = 1 ; long cnt_cnst = 0 ; for ( Map . Entry < Character , Integer > itr : freq . entrySet ( ) ) { if ( ! isVowel ( itr . getKey ( ) ) ) { denom *= fact ( itr . getValue ( ) ) ; cnt_cnst += itr . getValue ( ) ; } } long ans = fact ( cnt_cnst + 1 ) / denom ; return ( ans * vow ) ; } static long total_permutations ( HashMap < Character , Integer > freq ) { long cnt = 0 ; long denom = 1 ; for ( Map . Entry < Character , Integer > itr : freq . entrySet ( ) ) { denom *= fact ( itr . getValue ( ) ) ; cnt += itr . getValue ( ) ; } return fact ( cnt ) / denom ; } static long no_vowels_together ( String word ) { HashMap < Character , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { char ch = Character . toLowerCase ( word . charAt ( i ) ) ; if ( freq . containsKey ( ch ) ) { freq . put ( ch , freq . get ( ch ) + 1 ) ; } else { freq . put ( ch , 1 ) ; } } long total = total_permutations ( freq ) ; long vwl_tgthr = all_vowels_together ( freq ) ; long res = total - vwl_tgthr ; return res ; } public static void main ( String [ ] args ) { String word = " allahabad " ; long ans = no_vowels_together ( word ) ; System . out . println ( ans ) ; word = " geeksforgeeks " ; ans = no_vowels_together ( word ) ; System . out . println ( ans ) ; word = " abcd " ; ans = no_vowels_together ( word ) ; System . out . println ( ans ) ; } }