Make all strings from a given array equal by replacing minimum number of characters
Given an array of equal-length strings arr[], the task is to make all the strings of the array equal by replacing any character of a string with any other character, minimum number of times.
Examples:
Input: arr[] = { “west”, “east”, “wait” }
Output: 3
Explanation:
Replacing arr[0][1] with ‘a’ modifies arr[] to { “west”, “east”, “wait” }.
Replacing arr[1][0] with ‘w’ modifies arr[] to { “wast”, “wast”, “wait” }.
Replacing arr[2][2] with ‘s’ modifies arr[] to { “wast”, “wast”, “wast” }.
Therefore, the required output is 3.
Input: arr[] = { “abcd”, “bcde”, “cdef” }
Output: 8
Approach: The problem can be solved using Hashing. Follow the steps below to solve the problem:
- Initialize a 2D array, say hash[][], where hash[i][j] stores the frequency of the character i present at the jth index of all the strings.
- Traverse the array arr[] using variable i. For every ith string encountered, count the frequency of each distinct character of the string and store it into the hash[][] array.
- Initialize a variable, say cntMinOp, to store the minimum count of operations required to make all the strings of the array equal.
- Traverse the array hash[][] using variable i. For every ith column encountered, calculate the sum of the column, say Sum, the maximum element in the column, say Max, and update cntMinOp += (Sum – Max).
- Finally, print the value of cntMinOp.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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) << "\n" ;
}
|
Java
import java.util.*;
class GFG
{
static int minOperation(String arr[], int N)
{
int cntMinOP = 0 ;
int M = arr[ 0 ].length();
int [][]hash = new int [ 256 ][M];
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
{
hash[arr[i].charAt(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 = Math.max(Max, hash[j][i]);
}
cntMinOP += (Sum - Max);
}
return cntMinOP;
}
public static void main(String[] args)
{
String arr[] = { "abcd" , "bcde" , "cdef" };
int N = arr.length;
System.out.print(minOperation(arr, N)+ "\n" );
}
}
|
Python3
def minOperation(arr, N):
cntMinOP = 0 ;
M = len (arr[ 0 ]);
hash = [[ 0 for i in range (M)] for j in range ( 256 )];
for i in range (N):
for j in range (M):
hash [ ord (arr[i][j])][j] + = 1 ;
for i in range (M):
Sum = 0 ;
Max = 0 ;
for j in range ( 256 ):
Sum + = hash [j][i];
Max = max ( Max , hash [j][i]);
cntMinOP + = ( Sum - Max );
return cntMinOP;
if __name__ = = '__main__' :
arr = [ "abcd" , "bcde" , "cdef" ];
N = len (arr);
print (minOperation(arr, N));
|
C#
using System;
class GFG
{
static int minOperation(String []arr, int N)
{
int cntMinOP = 0;
int M = arr[0].Length;
int [,]hash = new int [256, M];
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 = Math.Max(Max, hash[j, i]);
}
cntMinOP += (Sum - Max);
}
return cntMinOP;
}
public static void Main(String[] args)
{
String []arr = { "abcd" , "bcde" , "cdef" };
int N = arr.Length;
Console.Write(minOperation(arr, N)+ "\n" );
}
}
|
Javascript
<script>
function minOperation(arr, N)
{
var cntMinOP = 0;
var M = arr[0].length;
var hash = Array.from(Array(256), ()=>Array(M).fill(0));
for ( var i = 0; i < N; i++) {
for ( var j = 0; j < M; j++) {
hash[arr[i][j].charCodeAt(0)][j]++;
}
}
for ( var i = 0; i < M; i++) {
var Sum = 0;
var Max = 0;
for ( var j = 0; j < 256; j++) {
Sum += hash[j][i];
Max = Math.max(Max, hash[j][i]);
}
cntMinOP += (Sum - Max);
}
return cntMinOP;
}
var arr = [ "abcd" , "bcde" , "cdef" ];
var N = arr.length;
document.write( minOperation(arr, N) + "<br>" );
</script>
|
Time Complexity:O(N * (M + 256)), where M is the length of the string
Auxiliary Space:O(M + 256)
Last Updated :
08 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...