📌  相关文章
📜  通过替换最少的字符数,使给定数组中的所有字符串相等

📅  最后修改于: 2021-05-07 05:01:30             🧑  作者: Mango

给予同等长度的字符串的常用3的阵列[]中,任务是使阵列的所有字符串通过用任何其他字符,次最小数目替换字符串的任何字符相等。

例子:

方法:可以使用哈希解决问题。请按照以下步骤解决问题:

  • 初始化一个2D数组,例如hash [] [] ,其中hash [i] [j]存储出现在所有字符串的j索引处的字符i的频率。
  • 使用变量i遍历数组arr [] 。对于每个i遇到字符串,计数字符串的每个不同的字符的频率,并将其存储到散[] []数组。
  • 初始化一个变量,例如cntMinOp ,以存储使数组的所有字符串相等所需的最小操作数。
  • 使用变量i遍历数组hash [] [] 。对于遇到的i列,计算列的总和,例如Sum ,列中的最大元素,例如Max ,并更新cntMinOp + =(Sum – Max)
  • 最后,打印cntMinOp的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the minimum count of
// operations required to make all strings
// equal by replacing characters of strings
int minOperation(string arr[], int N)
{
 
    // Stores minimum count of operations
    // required to make all strings equal
    int cntMinOP = 0;
 
    // Stores length of the string
    int M = arr[0].length();
 
    // hash[i][j]: Stores frequency of character
    // i present at j-th index of all strings
    int hash[256][M];
 
    // Initialize hash[][] to 0
    memset(hash, 0, sizeof(hash));
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // Iterate over characters of
        // current string
        for (int j = 0; j < M; j++) {
 
            // Update frequency of
            // arr[i][j]
            hash[arr[i][j]][j]++;
        }
    }
 
    // Traverse hash[][] array
    for (int i = 0; i < M; i++) {
 
        // Stores sum of i-th column
        int Sum = 0;
 
        // Stores the largest element
        // of i-th column
        int Max = 0;
 
        // Iterate over all possible
        // characters
        for (int j = 0; j < 256; j++) {
 
            // Update Sum
            Sum += hash[j][i];
 
            // Update Max
            Max = max(Max, hash[j][i]);
        }
 
        // Update cntMinOP
        cntMinOP += (Sum - Max);
    }
 
    return cntMinOP;
}
 
// Driver Code
int main()
{
 
    string arr[] = { "abcd", "bcde", "cdef" };
 
    int N = sizeof(arr) / sizeof(arr[0]);
    // Function call
    cout << minOperation(arr, N) << "\n";
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
// Function to find the minimum count of
// operations required to make all Strings
// equal by replacing characters of Strings
static int minOperation(String arr[], int N)
{
 
    // Stores minimum count of operations
    // required to make all Strings equal
    int cntMinOP = 0;
 
    // Stores length of the String
    int M = arr[0].length();
 
    // hash[i][j]: Stores frequency of character
    // i present at j-th index of all Strings
    int [][]hash = new int[256][M];
 
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
        // Iterate over characters of
        // current String
        for (int j = 0; j < M; j++)
        {
 
            // Update frequency of
            // arr[i][j]
            hash[arr[i].charAt(j)][j]++;
        }
    }
 
    // Traverse hash[][] array
    for (int i = 0; i < M; i++)
    {
 
        // Stores sum of i-th column
        int Sum = 0;
 
        // Stores the largest element
        // of i-th column
        int Max = 0;
 
        // Iterate over all possible
        // characters
        for (int j = 0; j < 256; j++)
        {
 
            // Update Sum
            Sum += hash[j][i];
 
            // Update Max
            Max = Math.max(Max, hash[j][i]);
        }
 
        // Update cntMinOP
        cntMinOP += (Sum - Max);
    }
 
    return cntMinOP;
}
 
// Driver Code
public static void main(String[] args)
{
    String arr[] = { "abcd", "bcde", "cdef" };
    int N = arr.length;
   
    // Function call
    System.out.print(minOperation(arr, N)+ "\n");
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python 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
def minOperation(arr, N):
   
    # Stores minimum count of operations
    # required to make all Strings equal
    cntMinOP = 0;
 
    # Stores length of the String
    M = len(arr[0]);
 
    # hash[i][j]: Stores frequency of character
    # i present at j-th index of all Strings
    hash = [[0 for i in range(M)] for j in range(256)];
 
    # Traverse the array arr
    for i in range(N):
 
        # Iterate over characters of
        # current String
        for j in range(M):
           
            # Update frequency of
            # arr[i][j]
            hash[ord(arr[i][j])][j] += 1;
 
    # Traverse hash array
    for i in range(M):
 
        # Stores sum of i-th column
        Sum = 0;
 
        # Stores the largest element
        # of i-th column
        Max = 0;
 
        # Iterate over all possible
        # characters
        for j in range(256):
           
            # Update Sum
            Sum += hash[j][i];
 
            # Update Max
            Max = max(Max, hash[j][i]);
 
        # Update cntMinOP
        cntMinOP += (Sum - Max);
    return cntMinOP;
 
# Driver Code
if __name__ == '__main__':
    arr = ["abcd", "bcde", "cdef"];
    N = len(arr);
 
    # Function call
    print(minOperation(arr, N));
 
    # This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
// Function to find the minimum count of
// operations required to make all Strings
// equal by replacing characters of Strings
static int minOperation(String []arr, int N)
{
 
    // Stores minimum count of operations
    // required to make all Strings equal
    int cntMinOP = 0;
 
    // Stores length of the String
    int M = arr[0].Length;
 
    // hash[i,j]: Stores frequency of character
    // i present at j-th index of all Strings
    int [,]hash = new int[256, M];
 
    // Traverse the array []arr
    for (int i = 0; i < N; i++)
    {
 
        // Iterate over characters of
        // current String
        for (int j = 0; j < M; j++)
        {
 
            // Update frequency of
            // arr[i,j]
            hash[arr[i][j], j]++;
        }
    }
 
    // Traverse hash[,] array
    for (int i = 0; i < M; i++)
    {
 
        // Stores sum of i-th column
        int Sum = 0;
 
        // Stores the largest element
        // of i-th column
        int Max = 0;
 
        // Iterate over all possible
        // characters
        for (int j = 0; j < 256; j++)
        {
 
            // Update Sum
            Sum += hash[j, i];
 
            // Update Max
            Max = Math.Max(Max, hash[j, i]);
        }
 
        // Update cntMinOP
        cntMinOP += (Sum - Max);
    }
    return cntMinOP;
}
 
// Driver Code
public static void Main(String[] args)
{
    String []arr = { "abcd", "bcde", "cdef" };
    int N = arr.Length;
   
    // Function call
    Console.Write(minOperation(arr, N)+ "\n");
}
}
 
// This code is contributed by 29AjayKumar


输出:
8

时间复杂度: O(N *(M + 256)),其中M是字符串的长度
辅助空间: O(M + 256)