Número de correcciones de caracteres en las strings dadas para que sean iguales

Dadas tres strings A , B y C . Cada uno de estos es una string de longitud N que consta de letras minúsculas en inglés. La tarea es hacer que todas las strings sean iguales realizando una operación en la que cualquier carácter de las strings dadas se pueda reemplazar con cualquier otro carácter, imprima el recuento del número mínimo de tales operaciones requeridas.

Ejemplos:  

Entrada: A = “lugar”, B = “abcde”, C = “plybe” 
Salida:
A = “lugar”, B = “abcde”, C = “plybe”. 
Podemos lograr la tarea en el número mínimo de operaciones realizando seis operaciones de la siguiente manera: 
Cambie el primer carácter en B a ‘p’. B ahora es “pbcde” 
Cambie el segundo carácter en B a ‘l’. B ahora es “plcde” 
Cambie el tercer carácter en B y C a ‘a’. B y C ahora son “plade” y “plabe” respectivamente. 
Cambie el cuarto carácter en B a ‘c’. B ahora es «lugar» 
Cambie el cuarto carácter en C a ‘c’. C ahora es «lugar»
Entrada: A = «juego», B = «juego», C = «juego» 
Salida: 0  

Enfoque: Ejecute un bucle, verifique si los i -ésimos caracteres de todas las strings son iguales, entonces no se requieren operaciones. Si dos caracteres son iguales, se requiere una operación y si los tres caracteres son diferentes, se requieren dos operaciones. 
 

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ implementation of the approach
#include <iostream>
#include <bits/stdc++.h>
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

<?php
// PHP implementation of the approach
 
// Function to return the count of 
// operations required
function minOperations($n, $a, $b, $c)
{
 
    // To store the count of operations
    $ans = 0;
    for ($i = 0; $i < $n; $i++)
    {
        $x = $a[$i];
        $y = $b[$i];
        $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
$a = "place";
$b = "abcde";
$c = "plybe";
$n = strlen($a);
echo minOperations($n, $a, $b, $c);
 
// This code is contributed by ajit.
?>

Javascript

<script>
    // Javascript implementation of the approach
     
    // Function to return the count of operations required
    function minOperations(n, a, b, c)
    {
   
        // To store the count of operations
        let ans = 0;
        for (let i = 0; i < n; i++)
        {
            let x = a[i];
            let y = b[i];
            let 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;
    }
     
    let a = "place";
    let b = "abcde";
    let c = "plybe";
    let n = a.length;
    document.write(minOperations(n, a, b, c));
 
</script>
Producción: 

6

 

Publicación traducida automáticamente

Artículo escrito por facebookruppal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *