Count different Bitwise OR values of equal length strings S1 and S2 by swapping exactly one pair of characters from the first string
Last Updated :
23 Jul, 2021
Given two binary strings S1 and S2, both of length N, the task is to count the number of different values of Bitwise OR other than the Bitwise OR of the original strings S1 and S2 by swapping exactly one pair of characters from the string S1.
Examples:
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
Approach: The given problem can be solved based on the following observations:
- If the same character is swapped in the string S1, then it will not affect the bitwise OR.
- If the different characters are swapped in the string S1 let say S1[i] = ‘0’ and S2[j] = ‘1’ then the bitwise OR of the value is changed as per the following rules:
- If S2[i] = ‘0’ and S2[j] = ‘0’.
- If S2[i] = ‘1’ and S2[j] = ‘0’.
- If S2[i] = ‘0’ and S2[j] = ‘1’.
From the above observations, Follow the below steps to solve the problem:
- Initialize four variable, say t00, t10, t11, t01 that stores the number of indexes i such that S1[i] = ‘0’ and S2[i] = ‘0’, S1[i] = ‘1’ and S2[i] = ‘0’, S1[i] = ‘1’ and S2[i] = ‘1’, and S1[i] = ‘0’ and S2[i] = ‘1’ respectively.
- Traverse the given strings S1 and S2, and increment the value of t00, t10, t11, t01 as per the following:
- If S1[i] = ‘0’ and S2[i] =’0′, then increment the value of t00 by 1.
- If S1[i] = ‘1’ and S2[i] = ‘0’, then increment the value of t10 by 1.
- If S1[i] = ‘1’ and S2[i] = ‘1’, then increment the value of t11 by 1.
- If S1[i] = ‘0’ and S2[i] = ‘1’, then increment the value of t01 by 1.
- After completing the above steps, print the value of t00 * t10 + t01 * t10 + t00 * t11 as the resultant number of swaps required having different Bitwise OR from the original Bitwise OR.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void differentBitwiseOR(string s1,
string s2)
{
int n = s1.size();
int t00 = 0, t10 = 0, t01 = 0, t11 = 0;
for ( int i = 0; i < n; i++) {
if (s1[i] == '0'
&& s2[i] == '0' ) {
t00++;
}
if (s1[i] == '1'
&& s2[i] == '0' ) {
t10++;
}
if (s1[i] == '1'
&& s2[i] == '1' ) {
t11++;
}
if (s1[i] == '0'
&& s2[i] == '1' ) {
t01++;
}
}
int ans = t00 * t10 + t01 * t10
+ t00 * t11;
cout << ans;
}
int main()
{
string S1 = "01001" ;
string S2 = "11011" ;
differentBitwiseOR(S1, S2);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void differentBitwiseOR(String s1, String s2)
{
int n = s1.length();
int t00 = 0 , t10 = 0 , t01 = 0 , t11 = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (s1.charAt(i) == '0' &&
s2.charAt(i) == '0' )
{
t00++;
}
if (s1.charAt(i) == '1' &&
s2.charAt(i) == '0' )
{
t10++;
}
if (s1.charAt(i) == '1' &&
s2.charAt(i) == '1' )
{
t11++;
}
if (s1.charAt(i) == '0' &&
s2.charAt(i) == '1' )
{
t01++;
}
}
int ans = t00 * t10 + t01 * t10 + t00 * t11;
System.out.print(ans);
}
public static void main(String[] args)
{
String S1 = "01001" ;
String S2 = "11011" ;
differentBitwiseOR(S1, S2);
}
}
|
Python3
def differentBitwiseOR(s1, s2):
n = len (s1)
t00 = 0
t10 = 0
t01 = 0
t11 = 0
for i in range (n):
if (s1[i] = = '0' and s2[i] = = '0' ):
t00 + = 1
if (s1[i] = = '1' and s2[i] = = '0' ):
t10 + = 1
if (s1[i] = = '1' and s2[i] = = '1' ):
t11 + = 1
if (s1[i] = = '0' and s2[i] = = '1' ):
t01 + = 1
ans = t00 * t10 + t01 * t10 + t00 * t11
print (ans)
S1 = "01001"
S2 = "11011"
differentBitwiseOR(S1, S2)
|
C#
using System;
class GFG{
static void differentBitwiseOR(String s1,
String s2)
{
int n = s1.Length;
int t00 = 0, t10 = 0, t01 = 0, t11 = 0;
for ( int i = 0; i < n; i++)
{
if (s1[i] == '0' && s2[i] == '0' )
{
t00++;
}
if (s1[i] == '1' && s2[i] == '0' )
{
t10++;
}
if (s1[i] == '1' && s2[i] == '1' )
{
t11++;
}
if (s1[i] == '0' && s2[i] == '1' )
{
t01++;
}
}
int ans = t00 * t10 + t01 * t10 + t00 * t11;
Console.Write(ans);
}
public static void Main()
{
String S1 = "01001" ;
String S2 = "11011" ;
differentBitwiseOR(S1, S2);
}
}
|
Javascript
<script>
function differentBitwiseOR(s1, s2) {
let n = s1.length;
let t00 = 0, t10 = 0, t01 = 0, t11 = 0;
for (let i = 0; i < n; i++) {
if (s1[i] == '0'
&& s2[i] == '0' ) {
t00++;
}
if (s1[i] == '1'
&& s2[i] == '0' ) {
t10++;
}
if (s1[i] == '1'
&& s2[i] == '1' ) {
t11++;
}
if (s1[i] == '0'
&& s2[i] == '1' ) {
t01++;
}
}
let ans = t00 * t10 + t01 * t10
+ t00 * t11;
document.write(ans);
}
let S1 = "01001" ;
let S2 = "11011" ;
differentBitwiseOR(S1, S2);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...