鉴于不相等的长度A和B的两个二进制字符串,该任务是打印二进制字符串这是A和B的异或运算。
例子:
Input: A = “11001”, B = “111111”
Output: 100110
Input: A = “11111”, B = “0”
Output: 11111
方法:想法是首先使两个长度相等的字符串,然后对每个字符进行XOR,然后将其存储在结果字符串。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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 strring
def addZeros(strr, n):
for i in range(n):
strr = "0" + strr
return strr
# Function to return the XOR
# of the given strrings
def getXOR(a, b):
# Lengths of the given strrings
aLen = len(a)
bLen = len(b)
# Make both the strrings 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
输出:
100110
时间复杂度: O(len),len = Max(长度a,长度b)
辅助空间: O(len)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。