XOR of all substrings of a given Binary String


A binary string is a string that contains only two different types of characters in it which are '0' and '1'. Substrings are strings that are formed by deleting some character from the given string from the beginning and from the ending (possibly zero or all). We are given a string and we have to get all the substrings of it and take XOR of it.

XOR is a bitwise operator that gives the result − if both the bits are the same then it will return zero otherwise 1.

Input 

string str = "10101"

Output 

XOR of all the substrings of the given binary string is: 11001

Explanation

For the last bit, we have three set bits, so there is 1, for the second and third last bits there are two set bits means there is 0, and for the first and the second bit, there is only a single set bit.

Input 

string str = "111"

Output 

110

Approach

We have seen the examples above, now let us move to the implementation part −

  • First, we will create a function that will return the required string and will take the given string as the parameter.

  • In the function, we will get the size of the string and then we will store the values in the array that will represent the value of ones impacting the current bit.

  • A current bit will contribute in all the substrings in which it is at the zeroth position, similarly for all the substrings in which it is at the first, second, and nth position it will make the impact that number of times.

  • We will traverse over the string and get the bits if the current total bits are odd then add '1' to the result otherwise '0'.

  • In the end, return the final answer and print in the main function.

Example

#include <bits/stdc++.h>
using namespace std; 

// function to get the XOR it will return the required string 
string getXOR(string str){
   int len = str.size(); // size of the string     
   // array to store the how any number of times current bit will occures at any position 
   int arr[len]; 
   int tot = 0; // variable to store the total sum     
   // traversing over the array to get the number of ones can present at the given position 
   for (int i = 0; i < len; i++){
      if (str[i] == '1'){
         arr[i] = i + 1; 
      } else {
         arr[i] = 0; 
      }
   }
   // calculating nth bit total occurrences
   tot = accumulate(arr, arr + len, 0);    
   string res = "";
   for (int i = 0; i < len; i++){
      if(tot & 1){
         res = "1" + res;
      } else {
         res = "0" + res;
      }
      tot -= arr[len-1-i];
   }
   return res;
}
int main(){
   string str = "10101"; // given string     
   // calling to the function 
   cout<<"XOR of all the substrings of the given binary string is: "<<getXOR(str)<<endl;    
   return 0;
}

Output

XOR of all the substrings of the given binary string is: 11001

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given string.

The space complexity of the above code is linear that is O(N) because we are using an array to store the count of the ones for each position.

Conclusion

In this tutorial, we have implemented a code to find the XOR for all the substrings of the given string. Substrings are strings that are formed by deleting some character from the given string from the beginning and from the ending (possibly zero or all). We have implemented a code with the linear time complexity that is O(N) and the same space complexity.

Updated on: 24-Aug-2023

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements