📜  使用XOR和OR进行字符串转换

📅  最后修改于: 2021-06-25 21:38:27             🧑  作者: Mango

给定两个二进制字符串。任务是检查是否可以通过多次执行给定的操作将字符串s1转换为字符串s2。

  • 选择字符串s1中的任意两个相邻字符,然后用a ^ b替换其中一个,用a替换另一个\oplus b(a OR b)。

例子:

方法:下面给出的表格解释了XOR和OR操作的所有可能性。

X Y X^Y X\oplus  Y
0 0 0 0
0 1 1 1
1 0 1 1
1 1 0 1

如果两个字符串都仅由0组成并且长度相同,则可以进行转换,因为两个相邻的零将仅产生零,而与对其进行的操作无关。如果两个字符串都为1,请按照以下步骤检查String1是否可以转换为String2。

  • 检查长度是否相等
  • 检查两个字符串是否都至少为1,因为如果两个字符串都至少为1,则可以进行所有转换,可以在表中看到

如果以上两个条件都成立,则可以将String1转换为String2。
下面是上述方法的实现:

C++
// C++ program to check if string1 can be
// converted to string2 using XOR and OR operations
#include 
using namespace std;
 
// function to check if conversion is possible or not
bool solve(string s1, string s2)
{
    bool flag1 = 0, flag2 = 0;
 
    // if lengths are different
    if (s1.length() != s2.length())
        return false;
     
    int l = s1.length();
 
    // iterate to check if both strings have 1
    for (int i = 0; i < l; i++) {
 
        // to check if there is
        // even one 1 in string s1
        if (s1[i] == '1')
            flag1 = 1;
 
        // to check if there is even
        // one 1 in string s2
        if (s2[i] == '1')
            flag2 = 1;
 
        if (flag1 && flag2)
            return true;
    }
     //if both strings have only '0'
     if(!flag1&&!flag2)
            return true;
    // if both string do not have a '1'.
    return false;
}
 
// Driver code
int main()
{
    string s1 = "100101";
    string s2 = "100000";
 
    if (solve(s1, s2))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program to check if
// string1 can be converted
// to string2 using XOR and
// OR operations
import java.io.*;
import java.util.*;
 
class GFG
{
     
// function to check if
// conversion is possible
// or not
static boolean solve(String s1,
                     String s2)
{
    boolean flag1 = false,
            flag2 = false;
 
    // if lengths are different
    if (s1.length() != s2.length())
        return false;
     
    int l = s1.length();
 
    // iterate to check if
    // both strings have 1
    for (int i = 0; i < l; i++)
    {
 
        // to check if there is
        // even one 1 in string s1
        if (s1.charAt(i) == '1')
            flag1 = true;
 
        // to check if there is even
        // one 1 in string s2
        if (s2.charAt(i) == '1')
            flag2 = true;
 
        if (flag1 == true &&
            flag2 == true)
            return true;
    }
     //if both strings have only '0'
     if(!flag1&&!flag2)
            return true;
    // if both string do
    // not have a '1'.
    return false;
}
 
// Driver code
public static void main(String args[])
{
    String s1 = "100101";
    String s2 = "100000";
 
    if (solve(s1, s2) == true)
        System.out.print("Yes");
    else
        System.out.print("No");
}
}


Python3
# Python3 program to check
# if string1 can be converted
# to string2 using XOR and
# OR operations
 
# function to check if
# conversion is possible or not
def solve(s1, s2):
    flag1 = 0
    flag2 = 0
 
# if lengths are different
    if (len(s1) != len(s2)):
        return False
     
    l = len(s1)
 
# iterate to check if
# both strings have 1
    for i in range (0, l):
 
    # to check if there is
    # even one 1 in string s1
        if (s1[i] == '1'):
            flag1 = 1;
 
    # to check if there is even
    # one 1 in string s2
        if (s2[i] == '1'):
            flag2 = 1
 
    # if both string
    # do not have a '1'.
        if (flag1 & flag2):
            return True
 
        if(!flag1 & !flag2):
            return True
    return False
 
# Driver code
s1 = "100101"
s2 = "100000"
 
if solve(s1, s2):
    print( "Yes")
else:
    print("No")
 
# This code is contributed
# by Shivi_Aggarwal


C#
// C# program to check if
// string1 can be converted
// to string2 using XOR and
// OR operations
using System;
 
class GFG
{
     
// function to check if
// conversion is possible
// or not
static bool solve(String s1,
                  String s2)
{
    bool flag1 = false,
         flag2 = false;
 
    // if lengths are different
    if (s1.Length != s2.Length)
        return false;
     
    int l = s1.Length;
 
    // iterate to check if
    // both strings have 1
    for (int i = 0; i < l; i++)
    {
 
        // to check if there is
        // even one 1 in string s1
        if (s1[i] == '1')
            flag1 = true;
 
        // to check if there is even
        // one 1 in string s2
        if (s2[i] == '1')
            flag2 = true;
 
        if (flag1 == true &&
            flag2 == true)
            return true;
    }
 
     //if both strings have only '0'
     if(!flag1&&!flag2)
            return true;
    // if both string do
    // not have a '1'.
    return false;
}
 
// Driver code
public static void Main()
{
    String s1 = "100101";
    String s2 = "100000";
 
    if (solve(s1, s2) == true)
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
Yes

时间复杂度: O(n),其中n是输入字符串的长度。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。