Minimize characters to be changed to make the left and right rotation of a string same | Function to find the minimum characters to be removed from the string ; Initialize answer by N ; If length is even ; Frequency array for odd and even indices ; Store the frequency of the characters at even and odd indices ; Stores the most occuring frequency for even and odd indices ; Update the answer ; If length is odd ; Stores the frequency of the characters of the string ; Stores the most occuring characterin the string ; Update the answer ; Driver Code
def getMinimumRemoval ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE ans = n NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT freqEven = { } NEW_LINE freqOdd = { } NEW_LINE for ch in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT freqEven [ chr ( ch ) ] = 0 NEW_LINE freqOdd [ chr ( ch ) ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT if str [ i ] in freqEven : NEW_LINE INDENT freqEven [ str [ i ] ] += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if str [ i ] in freqOdd : NEW_LINE INDENT freqOdd [ str [ i ] ] += 1 NEW_LINE DEDENT DEDENT DEDENT evenMax = 0 NEW_LINE oddMax = 0 NEW_LINE for ch in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT evenMax = max ( evenMax , freqEven [ chr ( ch ) ] ) NEW_LINE oddMax = max ( oddMax , freqOdd [ chr ( ch ) ] ) NEW_LINE DEDENT ans = ans - evenMax - oddMax NEW_LINE DEDENT else : NEW_LINE INDENT freq = { } NEW_LINE for ch in range ( ' a ' , ' z ' ) : NEW_LINE INDENT freq [ chr ( ch ) ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if str [ i ] in freq : NEW_LINE INDENT freq [ str [ i ] ] += 1 NEW_LINE DEDENT DEDENT strMax = 0 NEW_LINE for ch in range ( ' a ' , ' z ' ) : NEW_LINE INDENT strMax = max ( strMax , freq [ chr ( ch ) ] ) NEW_LINE DEDENT ans = ans - strMax NEW_LINE DEDENT return ans NEW_LINE DEDENT str = " geeksgeeks " NEW_LINE print ( getMinimumRemoval ( str ) ) NEW_LINE