Minimize changes to make all characters equal by changing vowel to consonant and vice versa | Function to find the minimum number of steps to make all characters equal ; Initializing the variables ; Store the frequency ; Iterate over the string ; Calculate the total number of vowels ; Storing frequency of each vowel ; Count the consonants ; Storing the frequency of each consonant ; Iterate over the 2 maps ; Maximum frequency of consonant ; Maximum frequency of vowel ; Find the result ; Driver Code
function operations ( s ) { let ans = 0 ; let vowels = 0 , consonants = 0 ; let max_vowels = Number . MIN_VALUE ; let max_consonants = Number . MIN_VALUE ; let freq_consonants = new Map ( ) ; let freq_vowels = new Map ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) == ' ' || s . charAt ( i ) == ' ' || s . charAt ( i ) == ' ' || s . charAt ( i ) == ' ' || s . charAt ( i ) == ' ' ) { vowels += 1 ; if ( freq_vowels . has ( s . charAt ( i ) ) ) { freq_vowels . set ( s . charAt ( i ) , freq_vowels . get ( s . charAt ( i ) ) + 1 ) } else { freq_vowels . set ( s . charAt ( i ) , 1 ) ; } } else { consonants += 1 ; if ( freq_consonants . has ( s . charAt ( i ) ) ) { freq_consonants . set ( s . charAt ( i ) , freq_consonants . get ( s . charAt ( i ) ) + 1 ) } else { freq_consonants . set ( s . charAt ( i ) , 1 ) ; } } } for ( let [ key , value ] of freq_consonants ) { max_consonants = Math . max ( max_consonants , value ) ; } for ( let [ key1 , value1 ] of freq_vowels ) { max_vowels = Math . max ( max_vowels , value1 ) ; } ans = Math . min ( ( 2 * ( vowels - max_vowels ) + consonants ) , ( 2 * ( consonants - max_vowels ) + consonants ) ) ; return ans ; } let S = " " ; document . write ( operations ( S ) ) ;