Open In App

Count valid Bitwise operation combinations for Binary Strings

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an odd integer N (N >= 3), a binary string S of length N and another string O of length (N-1)/2. Here, each character in string O can be one of the following: ‘&’, ‘|’ or ‘^’ representing Bitwise operations AND, OR, and XOR respectively, the task is to find the number of possible combinations for string O that satisfies the following conditions:

  • S[2] = S[0] O[0] S[1]
  • S[4] = S[2] O[1] S[3]
  • S[6] = S[4] O[2] S[5]
  • And so on, up to the last valid index of S.

Note: Output can be very large so modulo it by (10^9 + 7).

Examples:

Input: “110”
Output: 1
Explanation: The only operation string that satisfies the conditions is “^”. S[0] ^ S[1] = S[2], that is 1 ^ 1 = 0

Input: “10101″
Output: 4
Explanation: There can be four possible strings. They are:

  • “^^” : S[0] ^ S[1] = S[2], that is 1 ^ 0 = 1 and S[2] ^ S[3] = S[4], that is 1 ^ 0 = 1
  • “^|” : S[0] ^ S[1] = S[2], that is 1 ^ 0 = 1 and S[2] | S[3] = S[4], that is 1 | 0 = 1
  • “|^” : S[0] | S[1] = S[2], that is 1 | 0 = 1 and S[2] ^ S[3] = S[4], that is 1 ^ 0 = 1
  • ||” : S[0] | S[1] = S[2], that is 1 | 0 = 1 and S[2] | S[3] = S[4], that is 1 | 0 = 1

Approach: To solve the problem follow the below idea:

The problem can be solved by using a combination based approach where at every index i (i >= 2 and i is odd), we multiply our answer with the number of possible operators which we can use between S[i-2] and S[i-1] to get S[i]. Suppose, if at any index i, the possible operators are ‘^’ and ‘&’, then we multiply our answer with 2 and so on.

Below are the steps to solve the problem:

  • Initialize a variable ans = 1 to store the number of possible strings for O.
  • Iterate over the string S starting from index i = 2.
    • Maintain a variable cnt = 0, to count the number of possible operators for that index.
    • Check if we can use Bitwise AND for this iteration. If yes, increment cnt by 1.
    • Check if we can use Bitwise OR for this iteration. If yes, increment cnt by 1.
    • Check if we can use Bitwise XOR for this iteration. If yes, increment cnt by 1.
    • Multiply ans by cnt.
    • Increment index i by 2.
  • Return ans to get the number of all possible combinations for string O.

Below is the implementation for the above approach:

C++




// C++ Code for the above approach:
 
#include <bits/stdc++.h>
using namespace std;
 
// MOD value
const int MOD = 1e9 + 7;
 
// Function to process the string
long long getCount(string s)
{
 
    // Variable to store the number of
    // possible strings O
    long long ans = 1;
 
    // Iterate over string S starting
    // from index 2
    for (int i = 2; i < s.length(); i += 2) {
 
        // Store the operands as integers
        int x = s[i - 2] - '0';
        int y = s[i - 1] - '0';
        int z = s[i] - '0';
 
        // We need every equation as x # y = z
        // where # is any operator of {&, |, ^}
 
        // Variable to store the number of
        // valid operators
        int cnt = 0;
 
        // Bitwise-and '&'
        cnt += (x & y) == z;
 
        // Bitwise-or '|'
        cnt += (x | y) == z;
 
        // Bitwise-xor '^'
        cnt += (x ^ y) == z;
 
        // Combinations of the operators
        ans *= cnt;
        ans %= MOD;
    }
    return ans;
}
 
// Drivers code
int main()
{
 
    string s1 = "110";
    string s2 = "10101";
 
    // Function call to get the
    // count of strings
    cout << getCount(s1) << '\n';
    cout << getCount(s2) << '\n';
    return 0;
}


Java




// Java Code for the above approach
 
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // MOD value
    static final int MOD = 1000000007;
 
    // Function to process the string
    static long getCount(String s)
    {
        // variable to store the number of
        // possible strings O
        long ans = 1;
 
        // Iterate over the string S starting
        // from index 2
        for (int i = 2; i < s.length(); i += 2) {
            // Store the operands as ints
            int x = s.charAt(i - 2) - '0';
            int y = s.charAt(i - 1) - '0';
            int z = s.charAt(i) - '0';
 
            // We need every equation as x # y = z
            // where # is any operator of {&, |, ^}
 
            // variable to store the number of
            // valid operators
            int cnt = 0;
 
            // Bitwise-and '&'
            cnt += ((x & y) == z) ? 1 : 0;
 
            // Bitwise-or '|'
            cnt += ((x | y) == z) ? 1 : 0;
 
            // Bitwise-xor '^'
            cnt += ((x ^ y) == z) ? 1 : 0;
 
            // Combinations of the operators
            ans *= cnt;
            ans %= MOD;
        }
        return ans;
    }
 
    public static void main(String[] args)
    {
 
        String s1 = "110";
        String s2 = "10101";
 
        // Function call to get the
        // count of strings
        System.out.println(getCount(s1));
        System.out.println(getCount(s2));
    }
}


Python3




# Python Code for the above approach:
 
MOD = 10**9 + 7
 
# Function to process the string
 
 
def get_count(s):
    # variable to store the number of
    # possible strings O
    ans = 1
    # Iterate over the string S starting
    # from index 2
    for i in range(2, len(s), 2):
 
        # Store the operands as ints
        x = int(s[i - 2])
        y = int(s[i - 1])
        z = int(s[i])
        # We need every equation as x # y = z
        # where # is any operator of {&, |, ^}
 
        # variable to store the number of
        # valid operators
        cnt = 0
 
        # Bitwise-and '&'
        cnt += (x & y) == z
 
        # Bitwise-or '|'
        cnt += (x | y) == z
 
        # Bitwise-xor '^'
        cnt += (x ^ y) == z
 
        # Combinations of the operators
        ans *= cnt
        ans %= MOD
    return ans
 
 
s1 = "110"
s2 = "10101"
 
# Function call to get the count of strings
print(get_count(s1))
print(get_count(s2))


C#




// C# Code for the above approach
 
using System;
 
public class GFG {
 
    // MOD value
    const int MOD = 1000000007;
 
    // Function to process the string
    static long getCount(string s)
    {
        // variable to store the number of
        // possible strings O
        long ans = 1;
 
        // Iterate over the string S starting
        // from index 2
        for (int i = 2; i < s.Length; i += 2) {
            // Store the operands as ints
            int x = s[i - 2] - '0';
            int y = s[i - 1] - '0';
            int z = s[i] - '0';
 
            // We need every equation as x # y = z
            // where # is any operator of {&, |, ^}
 
            // variable to store the number of
            // valid operators
            int cnt = 0;
 
            // Bitwise-and '&'
            cnt += ((x & y) == z) ? 1 : 0;
 
            // Bitwise-or '|'
            cnt += ((x | y) == z) ? 1 : 0;
 
            // Bitwise-xor '^'
            cnt += ((x ^ y) == z) ? 1 : 0;
 
            // Combinations of the operators
            ans *= cnt;
            ans %= MOD;
        }
        return ans;
    }
 
    public static void Main()
    {
 
        string s1 = "110";
        string s2 = "10101";
 
        // Function call to get the
        // count of strings
        Console.WriteLine(getCount(s1));
        Console.WriteLine(getCount(s2));
    }
}
 
// This code is contributed by ragul21


Javascript




// Javascript Code for the above approach:
 
// Function to process the string
function getCount(s) {
    // Variable to store the number of possible strings O
    let ans = 1;
 
    // Iterate over the string S starting from index 2
    for (let i = 2; i < s.length; i += 2) {
        // Store the operands as integers
        const x = parseInt(s[i - 2]);
        const y = parseInt(s[i - 1]);
        const z = parseInt(s[i]);
 
        // We need every equation as x # y = z
        // where # is any operator of {&, |, ^}
 
        // Variable to store the number of valid operators
        let cnt = 0;
 
        // Bitwise-and '&'
        cnt += (x & y) === z ? 1 : 0;
 
        // Bitwise-or '|'
        cnt += (x | y) === z ? 1 : 0;
 
        // Bitwise-xor '^'
        cnt += (x ^ y) === z ? 1 : 0;
 
        // Combinations of the operators
        ans *= cnt;
        ans %= 1000000007; // MOD value
    }
    return ans;
}
 
const s1 = "110";
const s2 = "10101";
 
// Function call to get the count of strings
console.log(getCount(s1));
console.log(getCount(s2));


Output

1
4






Time Complexity: O(N), where N is the length of string S.
Auxiliary Space: O(1)



Similar Reads

Count of all valid combinations of at most K numbers that sum up to N
Given two numbers N and K, the task is to find the count of all valid combinations of at most K numbers that sum up to N such that the following conditions are true: Only numbers 1 through 9 are used.Each number is used at most once. Examples: Input: K = 3, N = 7Output: 5Explanation:1 2 41 62 53 47 Input: K = 3, N = 9Output: 8Explanation:1 2 61 3 5
7 min read
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND
Given an array arr[] consisting of N positive integers, replace pairs of array elements whose Bitwise AND exceeds Bitwise XOR values by their Bitwise AND value. Finally, count the maximum number of such pairs that can be generated from the array. Examples: Input: arr[] = {12, 9, 15, 7}Output: 2Explanation:Step 1: Select the pair {12, 15} and replac
9 min read
itertools.combinations() module in Python to print all possible combinations
Given an array of size n, generate and print all possible combinations of r elements in array. Examples: Input : arr[] = [1, 2, 3, 4], r = 2 Output : [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]Recommended: Please try your approach on {IDE} first, before moving on to the solution. This problem has existing recursive solution please refer Print
2 min read
Find all valid combinations of at most K numbers that sum up to N
Given two numbers N and K, the task is to find all valid combinations of at most K numbers that sum up to N such that the following conditions are true: Only numbers 1 through 9 are used.Each number is used at most once. Return a list of all possible valid combinations Examples: Input: K = 3, N = 7Output: 1 2 41 62 53 47 Input: K = 3, N = 9Output:
8 min read
Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
Given an array arr[] of size N. The task is to find the number of pairs (arr[i], arr[j]) as cntAND, cntOR, and cntXOR such that: cntAND: Count of pairs where bitwise AND of least significant bits is 1.cntOR: Count of pairs where bitwise OR of least significant bits is 1.cntXOR: Count of pairs where bitwise XOR of least significant bits is 1. Exampl
7 min read
Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
Given two integers X and Y, representing Bitwise XOR and Bitwise AND of two positive integers, the task is to calculate the Bitwise OR value of those two positive integers. Examples: Input: X = 5, Y = 2 Output: 7 Explanation: If A and B are two positive integers such that A ^ B = 5, A &amp; B = 2, then the possible value of A and B is 3 and 6 respe
7 min read
Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
Given a string S of valid parentheses sequence of length N and an even integer K, the task is to find the valid parentheses sequence of length K which is also a subsequence of the given string. Note: There can be more than one valid sequence, print any of them. Examples: Input: S = "()()()", K = 4Output: ()()Explanation:The string "()()" is a subse
7 min read
Count of binary strings of length N having equal count of 0's and 1's and count of 1's &ge; count of 0's in each prefix substring
Given an integer N, the task is to find the number of possible binary strings of length N with an equal frequency of 0's and 1's in which frequency of 1's are greater or equal to the frequency of 0's in every prefix substring. Examples: Input: N = 2Output: 1Explanation:All possible binary strings of length 2 are {"00", "01", "10" and "11"}. Out of
7 min read
Given two binary strings perform operation until B &gt; 0 and print the result
Given two binary strings A and B of length N and M (up to 105). The task is to repeat the below process and find the answer. Initialize ans = 0 while (B &gt; 0) ans += A &amp; B (bitwise AND) B = B / 2 print ans Note: Answer can be very large so print Answer % 1000000007. Examples: Input: A = "1001", B = "10101" Output: 11 1001 &amp; 10101 = 1, ans
10 min read
Count of valid pairs (X, Y) from given strings such that concatenating X with itself yields Y
Given an array arr[] of N strings. Let X and Y be two strings, X and Y are said to be valid pairs if the rearrangement of the resultant string from the concatenation of X with X (i.e., X+X) gives Y. The task is to count the number of such valid pairs. Examples: Input: N = 4, arr[] = {“hacker”, ”ackerhackerh”, ”int”, ”iittnn”, ”long”}Output: 2 Expla
7 min read