📌  相关文章
📜  给定字符串中的字符更正数以使它们相等

📅  最后修改于: 2022-05-13 01:55:37.487000             🧑  作者: Mango

给定字符串中的字符更正数以使它们相等

给定三个字符串ABC 。其中每一个都是由小写英文字母组成的长度为N的字符串。任务是通过执行一个操作使所有字符串相等,其中给定字符串的任何字符都可以替换为任何其他字符,打印所需的最小此类操作数的计数。

例子:

方法:运行一个循环,检查所有字符串的第 i字符是否相等,则不需要任何操作。如果两个字符相等,则需要一次操作,如果所有三个字符都不同,则需要两次操作。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#include 
using namespace std;
 
// Function to return the count of operations required
const int minOperations(int n, string a, string b, string c)
{
 
    // To store the count of operations
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        char x = a[i];
        char y = b[i];
        char z = c[i];
 
        // No operation required
        if (x == y && y == z)
            ;
 
        // One operation is required when
        // any two characters are equal
        else if (x == y || y == z || x == z)
        {
            ans++;
        }
             
        // Two operations are required when
        // none of the characters are equal
        else
        {
            ans += 2;
        }
    }
 
    // Return the minimum count of operations required
    return ans;
}
 
// Driver code
int main()
{
    string a = "place";
    string b = "abcde";
    string c = "plybe";
    int n = a.size();
    cout << minOperations(n, a, b, c);
    return 0;
}
 
// This code is contributed by 29AjayKumar


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the count of operations required
    static int minOperations(int n, String a, String b, String c)
    {
 
        // To store the count of operations
        int ans = 0;
        for (int i = 0; i < n; i++) {
            char x = a.charAt(i);
            char y = b.charAt(i);
            char z = c.charAt(i);
 
            // No operation required
            if (x == y && y == z)
                ;
 
            // One operation is required when
            // any two characters are equal
            else if (x == y || y == z || x == z) {
                ans++;
            }
 
            // Two operations are required when
            // none of the characters are equal
            else {
                ans += 2;
            }
        }
 
        // Return the minimum count of operations required
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String a = "place";
        String b = "abcde";
        String c = "plybe";
        int n = a.length();
        System.out.print(minOperations(n, a, b, c));
    }
}


Python3
# Python 3 implementation of the approach
 
# Function to return the count
# of operations required
def minOperations(n, a, b, c):
     
    # To store the count of operations
    ans = 0
    for i in range(n):
        x = a[i]
        y = b[i]
        z = c[i]
 
        # No operation required
        if (x == y and y == z):
            continue
 
        # One operation is required when
        # any two characters are equal
        elif (x == y or y == z or x == z):
            ans += 1
             
        # Two operations are required when
        # none of the characters are equal
        else:
            ans += 2
 
    # Return the minimum count
    # of operations required
    return ans
 
# Driver code
if __name__ == '__main__':
    a = "place"
    b = "abcde"
    c = "plybe"
    n = len(a)
    print(minOperations(n, a, b, c))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the count of operations required
    static int minOperations(int n, string a, string b, string c)
    {
 
        // To store the count of operations
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            char x = a[i];
            char y = b[i];
            char z = c[i];
 
            // No operation required
            if (x == y && y == z)
                {;}
 
            // One operation is required when
            // any two characters are equal
            else if (x == y || y == z || x == z)
            {
                ans++;
            }
 
            // Two operations are required when
            // none of the characters are equal
            else
            {
                ans += 2;
            }
        }
 
        // Return the minimum count of operations required
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        string a = "place";
        string b = "abcde";
        string c = "plybe";
        int n = a.Length;
        Console.Write(minOperations(n, a, b, c));
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
6