给定两个二进制字符串s1和s2。它们的XOR为X,任务是找到交换字符串s1中两位位置的方式的数量,以使新s1和s2之间形成的XOR与X不同。
例子:
Input: s1 = “01011”, s2 = “11001”
Output: 4
swap bits of index(1-based) (1, 4), (2, 3), (3, 4), or (3, 5) such that XOR value is changed.
Input: s1 = “011000”, s2 = “010011”
Output: 6
方法:
- 计算s1中1和0的数量。
- 遍历字符串s1,并检查以下两种情况:
- s1 [i]和s2 [i]中的0和0 ,用0代替1将改变XOR值。
- 在S1中1和0 [i]和S2 [I]中,与0代替1将改变异或值。
- 对于第一种情况,替换方式的数量将为已使用的1的数量。
- 对于第二种情况,替换方式的数量将是零的数量-已经使用的0。
- 两种情况下方式总数的总和就是答案。
下面是上述方法的实现:
C++
#include
using namespace std;
// Function that returns the number of
// bit swaps such that xor is different
int countWays(string s1, string s2)
{
int c1 = 0, c0 = 0;
int n = s1.length();
// traverse and count 1's and 0's
for (int i = 0; i < n; i++) {
if (s1[i] == '1')
c1++;
else
c0++;
}
int used1 = 0, used0 = 0;
int ways = 0;
// traverse in the string
for (int i = 0; i < n; i++) {
// if both positions are 0
if (s1[i] == '0' and s2[i] == '0') {
// add the number of ones as
// it will change the XOR
ways += c1;
// subtract the number of ones already used
ways -= used1;
// zeros have been used
used0++;
}
// when 1 and 0, to change XOR, we have to
// replace 1 by 0
else if (s1[i] == '1' and s2[i] == '0') {
// add number of 0's
ways += c0;
// subtract number of 0's already used
ways -= used0;
// count 1's used
used1++;
}
}
// return the answer
return ways;
}
// Driver Code
int main()
{
string s1 = "01011";
string s2 = "11001";
cout << countWays(s1, s2);
return 0;
}
Java
// Java Program to find Number of
// ways to change the XOR of two
// numbers by swapping the bits
class GFG
{
// Function that returns the
// number of bit swaps such
// that xor is different
static int countWays(String s1,
String s2)
{
int c1 = 0, c0 = 0;
int n = s1.length();
// traverse and count 1's and 0's
for (int i = 0; i < n; i++)
{
if (s1.charAt(i) == '1')
c1++;
else
c0++;
}
int used1 = 0, used0 = 0;
int ways = 0;
// traverse in the String
for (int i = 0; i < n; i++)
{
// if both positions are 0
if (s1.charAt(i) == '0' &&
s2.charAt(i) == '0')
{
// add the number of ones as
// it will change the XOR
ways += c1;
// subtract the number of
// ones already used
ways -= used1;
// zeros have been used
used0++;
}
// when 1 and 0, to change XOR,
// we have to replace 1 by 0
else if (s1.charAt(i) == '1' &&
s2.charAt(i) == '0')
{
// add number of 0's
ways += c0;
// subtract number of
// 0's already used
ways -= used0;
// count 1's used
used1++;
}
}
// return the answer
return ways;
}
// Driver Code
public static void main(String[] args)
{
String s1 = "01011";
String s2 = "11001";
System.out.println(countWays(s1, s2));
}
}
// This code is contributed
// by Arnab Kundu
Python3
# Function that returns the number of
# bit swaps such that xor is different
def countWays(s1, s2):
c1 = 0
c0 = 0
n = len(s1)
# traverse and count 1's and 0's
for i in range(0,n) :
if (s1[i] == '1'):
c1+=1
else:
c0+=1
used1 = 0
used0 = 0
ways = 0
# traverse in the string
for i in range(0,n) :
# if both positions are 0
if (s1[i] == '0' and s2[i] == '0') :
# add the number of ones as
# it will change the XOR
ways += c1
# subtract the number of ones already used
ways -= used1
# zeros have been used
used0+=1
# when 1 and 0, to change XOR, we have to
# replace 1 by 0
elif (s1[i] == '1' and s2[i] == '0') :
# add number of 0's
ways += c0
# subtract number of 0's already used
ways -= used0
# count 1's used
used1+=1
# return the answer
return ways
# Driver Code
if __name__=='__main__':
s1 = "01011"
s2 = "11001"
print(countWays(s1, s2))
# This code is contributed by Smitha Dinesh Semwal
C#
// C# Program to find Number of
// ways to change the XOR of two
// numbers by swapping the bits
using System;
class GFG
{
// Function that returns the
// number of bit swaps such
// that xor is different
static int countWays(String s1,
String s2)
{
int c1 = 0, c0 = 0;
int n = s1.Length;
// traverse and count 1's and 0's
for (int i = 0; i < n; i++)
{
if (s1[i] == '1')
c1++;
else
c0++;
}
int used1 = 0, used0 = 0;
int ways = 0;
// traverse in the String
for (int i = 0; i < n; i++)
{
// if both positions are 0
if (s1[i] == '0' &&
s2[i] == '0')
{
// add the number of ones as
// it will change the XOR
ways += c1;
// subtract the number of
// ones already used
ways -= used1;
// zeros have been used
used0++;
}
// when 1 and 0, to change XOR,
// we have to replace 1 by 0
else if (s1[i] == '1' &&
s2[i] == '0')
{
// add number of 0's
ways += c0;
// subtract number of
// 0's already used
ways -= used0;
// count 1's used
used1++;
}
}
// return the answer
return ways;
}
// Driver Code
public static void Main(String[] args)
{
String s1 = "01011";
String s2 = "11001";
Console.WriteLine(countWays(s1, s2));
}
}
// This code is contributed
// by Subhadeep Gupta
PHP
Javascript
输出:
4
时间复杂度: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。