通过从第一个字符串中交换一对字符来计算等长字符串S1 和 S2 的不同位或值
给定两个长度为N的二进制字符串S1和S2 ,任务是通过从字符串S1中交换一对字符来计算除原始字符串S1和S2的按位或之外的不同位或值的数量。
例子:
Input: S1 = “1100”, S2 = “0011”
Output: 4
Explanation: Bitwise OR of S1 and S2, if no swapping is performed is “1111”.
Below are swapping of characters performed to get the different values of Bitwise OR:
- If the characters at 0th and 2nd index are swapped, then string S1 modifies to “0110”. Now, the Bitwise OR of both the string is “0111”.
- If the characters at 0th and 3rd index are swapped, then string S1 modifies to “0101”. Now, the Bitwise OR of both the string is”0111″.
- If the characters at 1st and 2nd index are swapped, then string S1 modifies to “1010”. Now, the Bitwise OR of both the string is “1011”.
- If the characters at 1st and 3rd index are swapped, then string S1 modifies to “1001”. Now, the Bitwise OR of both the string is”1011″.
After the above steps, all the Bitwise-OR are different from the Bitwise OR of the original string. Therefore, the total count is 4.
Input: S1 = “01001”, S2 = “11011”
Output: 2
方法:可以根据以下观察解决给定的问题:
- 如果在字符串S1中交换相同的字符,则不会影响按位或。
- 如果在字符串S1中交换了不同的字符,假设S1[i] = '0'和S2[j] = '1'则值的按位或将根据以下规则进行更改:
- 如果S2[i] = '0'和S2[j] = '0' 。
- 如果S2[i] = '1'和S2[j] = '0' 。
- 如果S2[i] = '0'和S2[j] = '1' 。
根据以上观察,请按照以下步骤解决问题:
- 初始化四个变量,比如t00 、 t10 、 t11 、 t01 ,它们存储索引i的数量,使得S1[i] = '0'和S2[i] = '0' , S1[i] = '1'和S2[ i] = '0' , S1[i] = '1'和S2[i] = '1' , S1[i] = '0'和S2[i] = '1'分别。
- 遍历给定的字符串S1和S2 ,并按以下方式递增t00 、 t10 、 t11 、 t01的值:
- 如果S1[i] = '0'和S2[i] ='0' ,则将t00的值增加1 。
- 如果S1[i] = '1'和S2[i] = '0' ,则将t10的值增加1 。
- 如果S1[i] = '1'和S2[i] = '1' ,则将t11的值增加1 。
- 如果S1[i] = '0'和S2[i] = '1' ,则将t01的值增加1 。
- 完成上述步骤后,打印t00 * t10 + t01 * t10 + t00 * t11的值作为与原始位或不同的位或所需的交换次数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of ways
// to obtain different Bitwise OR
void differentBitwiseOR(string s1,
string s2)
{
int n = s1.size();
// Stores the count of pairs t00,
// t10, t01, t11
int t00 = 0, t10 = 0, t01 = 0, t11 = 0;
// Traverse the characters of
// the string S1 and S2
for (int i = 0; i < n; i++) {
// Count the pair (0, 0)
if (s1[i] == '0'
&& s2[i] == '0') {
t00++;
}
// Count the pair (1, 0)
if (s1[i] == '1'
&& s2[i] == '0') {
t10++;
}
// Count the pair (1, 1)
if (s1[i] == '1'
&& s2[i] == '1') {
t11++;
}
// Count the pair (0, 1)
if (s1[i] == '0'
&& s2[i] == '1') {
t01++;
}
}
// Number of ways to calculate the
// different bitwise OR
int ans = t00 * t10 + t01 * t10
+ t00 * t11;
// Print the result
cout << ans;
}
// Driver Code
int main()
{
string S1 = "01001";
string S2 = "11011";
differentBitwiseOR(S1, S2);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the number of ways
// to obtain different Bitwise OR
static void differentBitwiseOR(String s1, String s2)
{
int n = s1.length();
// Stores the count of pairs t00,
// t10, t01, t11
int t00 = 0, t10 = 0, t01 = 0, t11 = 0;
// Traverse the characters of
// the string S1 and S2
for(int i = 0; i < n; i++)
{
// Count the pair (0, 0)
if (s1.charAt(i) == '0' &&
s2.charAt(i) == '0')
{
t00++;
}
// Count the pair (1, 0)
if (s1.charAt(i) == '1' &&
s2.charAt(i) == '0')
{
t10++;
}
// Count the pair (1, 1)
if (s1.charAt(i) == '1' &&
s2.charAt(i) == '1')
{
t11++;
}
// Count the pair (0, 1)
if (s1.charAt(i) == '0' &&
s2.charAt(i) == '1')
{
t01++;
}
}
// Number of ways to calculate the
// different bitwise OR
int ans = t00 * t10 + t01 * t10 + t00 * t11;
// Print the result
System.out.print(ans);
}
// Driver Code
public static void main(String[] args)
{
String S1 = "01001";
String S2 = "11011";
differentBitwiseOR(S1, S2);
}
}
// This code is contributed by subhammahato348
Python3
# Python program for the above approach
# Function to find the number of ways
# to obtain different Bitwise OR
def differentBitwiseOR(s1, s2):
n = len(s1)
# Stores the count of pairs t00,
# t10, t01, t11
t00 = 0
t10 = 0
t01 = 0
t11 = 0
# Traverse the characters of
# the string S1 and S2
for i in range(n):
# Count the pair (0, 0)
if (s1[i] == '0' and s2[i] == '0'):
t00 += 1
# Count the pair (1, 0)
if (s1[i] == '1' and s2[i] == '0'):
t10 += 1
# Count the pair (1, 1)
if (s1[i] == '1' and s2[i] == '1'):
t11 += 1
# Count the pair (0, 1)
if (s1[i] == '0' and s2[i] == '1'):
t01 += 1
# Number of ways to calculate the
# different bitwise OR
ans = t00 * t10 + t01 * t10 + t00 * t11
# Print the result
print(ans)
# Driver Code
S1 = "01001"
S2 = "11011"
differentBitwiseOR(S1, S2)
# This code is contributed by _saurabh_jaiswal
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the number of ways
// to obtain different Bitwise OR
static void differentBitwiseOR(String s1,
String s2)
{
int n = s1.Length;
// Stores the count of pairs t00,
// t10, t01, t11
int t00 = 0, t10 = 0, t01 = 0, t11 = 0;
// Traverse the characters of
// the string S1 and S2
for(int i = 0; i < n; i++)
{
// Count the pair (0, 0)
if (s1[i] == '0' && s2[i] == '0')
{
t00++;
}
// Count the pair (1, 0)
if (s1[i] == '1' && s2[i] == '0')
{
t10++;
}
// Count the pair (1, 1)
if (s1[i] == '1' && s2[i] == '1')
{
t11++;
}
// Count the pair (0, 1)
if (s1[i] == '0' && s2[i] == '1')
{
t01++;
}
}
// Number of ways to calculate the
// different bitwise OR
int ans = t00 * t10 + t01 * t10 + t00 * t11;
// Print the result
Console.Write(ans);
}
// Driver Code
public static void Main()
{
String S1 = "01001";
String S2 = "11011";
differentBitwiseOR(S1, S2);
}
}
// This code is contributed by subhammahato348
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)