XOR of all substrings of a given Binary String
Last Updated :
31 May, 2022
Given a binary string str of size N, the task is to calculate the bitwise XOR of all substrings of str.
Examples:
Input: str = “11”
Output: 11
Explanation: The substrings of “11” are: 1, 1, and 11.
Their XOR = 1 ⊕ 1 ⊕ 11 = 11
Input: str = “110”
Output: 111
Explanation: The substrings of 110 are: 1, 1, 0, 11, 10, 110.
Their XOR = 1 ⊕ 1 ⊕ 0 ⊕ 11 ⊕ 10 ⊕ 110 = 111
Input: str = “10101”
Output: 11001
Explanation: The substrings of 10101 are: 1, 10, 101, 1010, 10101, 0, 01, 010, 0101, 1, 10, 101, 0, 01, and 1.
Their XOR = 1 ⊕ 10 ⊕ 101 ⊕ 1010 ⊕ 10101 ⊕ 0 ⊕ 01 ⊕ 010 ⊕ 0101 ⊕ 1 ⊕ 10 ⊕ 101 ⊕ 0 ⊕ 01 ⊕ 1 = 11001
Approach: This problem can be solved based on the following observation:
XOR of odd number of 1s is always 1. Otherwise, the XOR is 0.
Each jth bit can be the ith bit in a substring when 0 ≤ j ≤ N-i.
So each character has contribution for the last bit (LSB) of the result.
All characters from i = 0 to N-2 has contribution for 2nd last bit and so on.
Follow the steps mentioned below to utilize the above observation to solve this problem:
- Create an array (say occurrence[]) to store the total count of 1s having contribution for ith index from LSB end in resultant XOR.
- Now iterate from the LSB end (iterator i):
- If the total number of 1s at ith index is odd then the resultant XOR will have ith bit from the LSB end as 1.
- Otherwise, the value in ith bit will be 0.
- Return the resultant XOR.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string totalXOR(string s, int n)
{
vector< int > occurrence;
for ( int i = 0; i < n; i++) {
if (s[i] == '1' )
occurrence.push_back(i + 1);
else
occurrence.push_back(0);
}
int sums
= accumulate(occurrence.begin(),
occurrence.end(), 0);
string ans = "" ;
for ( int i = 0; i < n; i++) {
if (sums % 2 == 1)
ans = "1" + ans;
else
ans = "0" + ans;
sums -= occurrence.back();
occurrence.pop_back();
}
return ans;
}
int main()
{
int N = 5;
string str = "10101" ;
cout << totalXOR(str, N);
return 0;
}
|
Java
import java.io.*;
import java.util.ArrayList;
public class GFG {
static String totalXOR(String s, int n)
{
ArrayList<Integer> occurrence
= new ArrayList<Integer>();
for ( int i = 0 ; i < n; i++) {
if ((s.charAt(i)) == '1' )
occurrence.add(i + 1 );
else
occurrence.add( 0 );
}
int sums = 0 ;
for ( int i : occurrence)
sums += i;
String ans = "" ;
for ( int i = 0 ; i < n; i++) {
if (sums % 2 == 1 )
ans = "1" + ans;
else
ans = "0" + ans;
sums -= occurrence.get(occurrence.size() - 1 );
occurrence.remove(occurrence.size() - 1 );
}
return ans;
}
public static void main(String[] args)
{
int N = 5 ;
String str = "10101" ;
System.out.print(totalXOR(str, N));
}
}
|
Python3
def totalXOR(string, n):
occurrences = [i + 1 if string[i] = =
'1' else 0 for i in range (n)]
sums = sum (occurrences)
ans = ''
for i in range (n):
ans \
= [ '0' , '1' ][sums % 2 = = 1 ] + ans
sums - = occurrences[ - 1 ]
del occurrences[ - 1 ]
return ans
if __name__ = = '__main__' :
N = 5
str = "10101"
print (totalXOR( str , N))
|
C#
using System;
using System.Collections;
public class GFG{
static string totalXOR( string s, int n)
{
ArrayList occurrence
= new ArrayList();
for ( int i = 0; i < n; i++) {
if (s[i] == '1' )
occurrence.Add(i + 1);
else
occurrence.Add(0);
}
int sums = 0;
foreach ( var i in occurrence)
sums = sums + ( int )i;
string ans = "" ;
for ( int i = 0; i < n; i++) {
if (sums % 2 == 1)
ans = "1" + ans;
else
ans = "0" + ans;
sums = sums - ( int )occurrence[occurrence.Count - 1];
occurrence.RemoveAt(occurrence.Count - 1);
}
return ans;
}
static public void Main (){
int N = 5;
string str = "10101" ;
Console.Write(totalXOR(str, N));
}
}
|
Javascript
<script>
const totalXOR = (s, n) => {
let occurrence = [];
for (let i = 0; i < n; i++) {
if (s[i] == '1' )
occurrence.push(i + 1);
else
occurrence.push(0);
}
let sums = 0;
for (let itm in occurrence) sums += occurrence[itm];
let ans = "" ;
for (let i = 0; i < n; i++) {
if (sums % 2 == 1)
ans = "1" + ans;
else
ans = "0" + ans;
sums -= occurrence[occurrence.length - 1];
occurrence.pop();
}
return ans;
}
let N = 5;
let str = "10101" ;
document.write(totalXOR(str, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...