XOR dos strings binarias de longitudes desiguales

Dadas dos strings binarias de longitudes desiguales A y B , la tarea es imprimir la string binaria que es el XOR de A y B.
Ejemplos: 

Entrada: A = “11001”, B = “111111” 
Salida: 100110
Entrada: A = “11111”, B = “0” 
Salida: 11111 

Enfoque: La idea es primero hacer que ambas strings tengan la misma longitud y luego realizar el XOR de cada carácter uno por uno y almacenarlo en la string resultante.
A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to insert n 0s in the
// beginning of the given string
void addZeros(string& str, int n)
{
    for (int i = 0; i < n; i++) {
        str = "0" + str;
    }
}
 
// Function to return the XOR
// of the given strings
string getXOR(string a, string b)
{
 
    // Lengths of the given strings
    int aLen = a.length();
    int bLen = b.length();
 
    // Make both the strings of equal lengths
    // by inserting 0s in the beginning
    if (aLen > bLen) {
        addZeros(b, aLen - bLen);
    }
    else if (bLen > aLen) {
        addZeros(a, bLen - aLen);
    }
 
    // Updated length
    int len = max(aLen, bLen);
 
    // To store the resultant XOR
    string res = "";
    for (int i = 0; i < len; i++) {
        if (a[i] == b[i])
            res += "0";
        else
            res += "1";
    }
 
    return res;
}
 
// Driver code
int main()
{
    string a = "11001", b = "111111";
 
    cout << getXOR(a, b);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
     
    // Function to insert n 0s in the
    // beginning of the given string
    static String addZeros(String str, int n)
    {
        for (int i = 0; i < n; i++)
        {
            str = "0" + str;
        }
        return str;
    }
     
    // Function to return the XOR
    // of the given strings
    static String getXOR(String a, String b)
    {
     
        // Lengths of the given strings
        int aLen = a.length();
        int bLen = b.length();
     
        // Make both the strings of equal lengths
        // by inserting 0s in the beginning
        if (aLen > bLen)
        {
            a = addZeros(b, aLen - bLen);
        }
        else if (bLen > aLen)
        {
            a = addZeros(a, bLen - aLen);
        }
     
        // Updated length
        int len = Math.max(aLen, bLen);
     
        // To store the resultant XOR
        String res = "";
         
        for (int i = 0; i < len; i++)
        {
            if (a.charAt(i) == b.charAt(i))
                res += "0";
            else
                res += "1";
        }
        return res;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        String a = "11001", b = "111111";
     
        System.out.println(getXOR(a, b));
    }
}
 
// This code is contributed by AnkitRai01

Python3

# Python3 implementation of the approach
 
# Function to insert n 0s in the
# beginning of the given string
def addZeros(strr, n):
    for i in range(n):
        strr = "0" + strr
    return strr
 
# Function to return the XOR
# of the given strings
def getXOR(a, b):
 
    # Lengths of the given strings
    aLen = len(a)
    bLen = len(b)
 
    # Make both the strings of equal lengths
    # by inserting 0s in the beginning
    if (aLen > bLen):
        b = addZeros(b, aLen - bLen)
    elif (bLen > aLen):
        a = addZeros(a, bLen - aLen)
 
    # Updated length
    lenn = max(aLen, bLen);
 
    # To store the resultant XOR
    res = ""
    for i in range(lenn):
        if (a[i] == b[i]):
            res += "0"
        else:
            res += "1"
 
    return res
 
# Driver code
a = "11001"
b = "111111"
 
print(getXOR(a, b))
 
# This code is contributed by Mohit Kumar

C#

// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to insert n 0s in the
    // beginning of the given string
    static String addZeros(String str, int n)
    {
        for (int i = 0; i < n; i++)
        {
            str = "0" + str;
        }
        return str;
    }
     
    // Function to return the XOR
    // of the given strings
    static String getXOR(String a, String b)
    {
     
        // Lengths of the given strings
        int aLen = a.Length;
        int bLen = b.Length;
     
        // Make both the strings of equal lengths
        // by inserting 0s in the beginning
        if (aLen > bLen)
        {
            a = addZeros(b, aLen - bLen);
        }
        else if (bLen > aLen)
        {
            a = addZeros(a, bLen - aLen);
        }
     
        // Updated length
        int len = Math.Max(aLen, bLen);
     
        // To store the resultant XOR
        String res = "";
         
        for (int i = 0; i < len; i++)
        {
            if (a[i] == b[i])
                res += "0";
            else
                res += "1";
        }
        return res;
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        String a = "11001", b = "111111";
     
        Console.WriteLine(getXOR(a, b));
    }
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
    // Javascript implementation of the approach
     
    // Function to insert n 0s in the
    // beginning of the given string
    function addZeros(str, n)
    {
        for (let i = 0; i < n; i++)
        {
            str = "0" + str;
        }
        return str;
    }
      
    // Function to return the XOR
    // of the given strings
    function getXOR(a, b)
    {
      
        // Lengths of the given strings
        let aLen = a.length;
        let bLen = b.length;
      
        // Make both the strings of equal lengths
        // by inserting 0s in the beginning
        if (aLen > bLen)
        {
            a = addZeros(b, aLen - bLen);
        }
        else if (bLen > aLen)
        {
            a = addZeros(a, bLen - aLen);
        }
      
        // Updated length
        let len = Math.max(aLen, bLen);
      
        // To store the resultant XOR
        let res = "";
          
        for (let i = 0; i < len; i++)
        {
            if (a[i] == b[i])
                res += "0";
            else
                res += "1";
        }
        return res;
    }
     
    let a = "11001", b = "111111";
      
      document.write(getXOR(a, b));
     
    // This code is contributed by divyeshrabadiya07.
</script>
Producción: 

100110

 

Complejidad de tiempo: O(len), len=Max(longitud a,longitud b)

Espacio Auxiliar: O(len)

Publicación traducida automáticamente

Artículo escrito por skylags 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 *