Count minimum character replacements required such that given string satisfies the given conditions | Function that finds the minimum count of steps required to make the string special ; Stores the frequency of the left & right half of string ; Find frequency of left half ; Find frequency of left half ; Make all characters equal to character c ; Case 1 : For s [ i ] < s [ j ] ; Subtract all the characters on left side that are <= d ; Adding all characters on the right side that same as d ; Find minimum value of count ; Similarly for Case 2 : s [ i ] > s [ j ] ; Return the minimum changes ; Driver Code ; Given string S ; Function Call
def minChange ( s , n ) : NEW_LINE INDENT L = [ 0 ] * 26 ; NEW_LINE R = [ 0 ] * 26 ; NEW_LINE for i in range ( 0 , n // 2 ) : NEW_LINE INDENT ch = s [ i ] ; NEW_LINE L [ ord ( ch ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( n // 2 , n ) : NEW_LINE INDENT ch = s [ i ] ; NEW_LINE R [ ord ( ch ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT count = n ; NEW_LINE for ch in range ( ord ( ' a ' ) , ord ( ' z ' ) ) : NEW_LINE INDENT count = min ( count , n - L [ ch - ord ( ' a ' ) ] - R [ ch - ord ( ' a ' ) ] ) ; NEW_LINE DEDENT change = n / 2 ; NEW_LINE for d in range ( 0 , 25 ) : NEW_LINE INDENT change -= L [ d ] ; NEW_LINE change += R [ d ] ; NEW_LINE count = min ( count , change ) ; NEW_LINE DEDENT change = n / 2 ; NEW_LINE for d in range ( 0 , 25 ) : NEW_LINE INDENT change -= R [ d ] ; NEW_LINE change += L [ d ] ; NEW_LINE count = min ( change , count ) ; NEW_LINE DEDENT return int ( count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aababc " ; NEW_LINE N = len ( S ) ; NEW_LINE print ( minChange ( S , N ) ) ; NEW_LINE DEDENT