Make all strings from a given array equal by replacing minimum number of characters | Function to find the minimum count of operations required to make all Strings equal by replacing characters of Strings ; Stores minimum count of operations required to make all Strings equal ; Stores length of the String ; hash [ i ] [ j ] : Stores frequency of character i present at j - th index of all Strings ; Traverse the array arr ; Iterate over characters of current String ; Update frequency of arr [ i ] [ j ] ; Traverse hash array ; Stores sum of i - th column ; Stores the largest element of i - th column ; Iterate over all possible characters ; Update Sum ; Update Max ; Update cntMinOP ; Driver Code ; Function call
def minOperation ( arr , N ) : NEW_LINE INDENT cntMinOP = 0 ; NEW_LINE M = len ( arr [ 0 ] ) ; NEW_LINE hash = [ [ 0 for i in range ( M ) ] for j in range ( 256 ) ] ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT hash [ ord ( arr [ i ] [ j ] ) ] [ j ] += 1 ; NEW_LINE DEDENT DEDENT for i in range ( M ) : NEW_LINE INDENT Sum = 0 ; NEW_LINE Max = 0 ; NEW_LINE for j in range ( 256 ) : NEW_LINE INDENT Sum += hash [ j ] [ i ] ; NEW_LINE Max = max ( Max , hash [ j ] [ i ] ) ; NEW_LINE DEDENT cntMinOP += ( Sum - Max ) ; NEW_LINE DEDENT return cntMinOP ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " abcd " , " bcde " , " cdef " ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( minOperation ( arr , N ) ) ; NEW_LINE DEDENT