Minimize changes to make all characters equal by changing vowel to consonant and vice versa | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int operations ( string s ) { int ans = 0 ; int vowels = 0 , consonants = 0 ; int max_vowels = INT_MIN ; int max_consonants = INT_MIN ; unordered_map < char , int > freq_consonants ; unordered_map < char , int > freq_vowels ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == ' a ' or s [ i ] == ' e ' or s [ i ] == ' i ' or s [ i ] == ' o ' or s [ i ] == ' u ' ) { vowels += 1 ; freq_vowels [ s [ i ] ] += 1 ; } else { consonants += 1 ; freq_consonants [ s [ i ] ] += 1 ; } } for ( auto it = freq_consonants . begin ( ) ; it != freq_consonants . end ( ) ; it ++ ) { max_consonants = max ( max_consonants , it -> second ) ; } for ( auto it = freq_vowels . begin ( ) ; it != freq_vowels . end ( ) ; it ++ ) { max_vowels = max ( max_vowels , it -> second ) ; } ans = min ( ( 2 * ( vowels - max_vowels ) + consonants ) , ( 2 * ( consonants - max_vowels ) + consonants ) ) ; return ans ; } int main ( ) { string S = " geeksforgeeks " ; cout << operations ( S ) ; return 0 ; }