Minimize changes to make all characters equal by changing vowel to consonant and vice versa | Python 3 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
import sys NEW_LINE from collections import defaultdict NEW_LINE def operations ( s ) : NEW_LINE INDENT ans = 0 NEW_LINE vowels = 0 NEW_LINE consonants = 0 NEW_LINE max_vowels = - sys . maxsize - 1 NEW_LINE max_consonants = - sys . maxsize - 1 NEW_LINE freq_consonants = defaultdict ( int ) NEW_LINE freq_vowels = defaultdict ( int ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' a ' or s [ i ] == ' e ' or s [ i ] == ' i ' or s [ i ] == ' o ' or s [ i ] == ' u ' ) : NEW_LINE INDENT vowels += 1 NEW_LINE freq_vowels [ s [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT consonants += 1 NEW_LINE freq_consonants [ s [ i ] ] += 1 NEW_LINE DEDENT DEDENT for it in freq_consonants : NEW_LINE INDENT max_consonants = max ( max_consonants , freq_consonants [ it ] ) NEW_LINE DEDENT for it in freq_vowels : NEW_LINE INDENT max_vowels = max ( max_vowels , freq_vowels [ it ] ) NEW_LINE DEDENT ans = min ( ( 2 * ( vowels - max_vowels ) + consonants ) , ( 2 * ( consonants - max_vowels ) + consonants ) ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE print ( operations ( S ) ) NEW_LINE DEDENT