Make all strings from a given array equal by replacing minimum number of characters | C ++ program to implement the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( string arr [ ] , int N ) { int cntMinOP = 0 ; int M = arr [ 0 ] . length ( ) ; int hash [ 256 ] [ M ] ; memset ( hash , 0 , sizeof ( hash ) ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] [ j ] ] [ j ] ++ ; } } for ( int i = 0 ; i < M ; i ++ ) { int Sum = 0 ; int Max = 0 ; for ( int j = 0 ; j < 256 ; j ++ ) { Sum += hash [ j ] [ i ] ; Max = max ( Max , hash [ j ] [ i ] ) ; } cntMinOP += ( Sum - Max ) ; } return cntMinOP ; } int main ( ) { string arr [ ] = { " abcd " , " bcde " , " cdef " } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperation ( arr , N ) << " STRNEWLINE " ; }