Open In App

Count different Bitwise OR values of equal length strings S1 and S2 by swapping exactly one pair of characters from the first string

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:

From the above observations, Follow the below steps to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
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 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




# 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# 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




<script>
  
        // JavaScript program for the above approach
 
        // Function to find the number of ways
        // to obtain different Bitwise OR
        function differentBitwiseOR(s1, s2) {
            let n = s1.length;
 
            // Stores the count of pairs t00,
            // t10, t01, t11
            let t00 = 0, t10 = 0, t01 = 0, t11 = 0;
 
            // Traverse the characters of
            // the string S1 and S2
            for (let 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
            let ans = t00 * t10 + t01 * t10
                + t00 * t11;
 
            // Print the result
            document.write(ans);
        }
 
        // Driver Code
        let S1 = "01001";
        let S2 = "11011";
        differentBitwiseOR(S1, S2);
 
 
 
 
  // This code is contributed by Potta Lokesh
   
</script>

Output: 
2

 

Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :