# Count of cyclic permutations having XOR with other binary string as 0 in C++

C++Server Side ProgrammingProgramming

We are given with two binary strings let's say str_1 and str_2 containing the combination of 1's and 0's and the task is to firstly form the set let's say “SET” of different permutations possible from the string str_1 and then we will perform XOR operations of the elements in set with the binary string str_2 and then check whether the XOR is returning 0 or not. If yes, then consider the case else ignore it.

## Let us understand with examples.

For Example

Input -  string str_1 = "1111", string str_2 = "1111"

Output - Count of cyclic permutations having XOR with other binary string as 0 are: 4

Explanation - We will create the set using string str_2 and set will be {1111}. Now we will perform the XOR operations using string str_1 and set formed so {1111} ^ “1111” = 0. Since we have 4 similar elements in string str_2 therefore we can form 4 different permutations therefore the output is 4.

Input -  string str_1 = "1101", string str_2 = "1101"

Output - Count of cyclic permutations having XOR with other binary string as 0 are: 1

Explanation - We will create the set using string str_2 and the set will be {1101, 1110, 1011, 0111}. Now we will perform the XOR operations using string str_1 and set formed i.e.

{1101} ^ 1101 = 0

{1110} ^ 1101 not equals 0

{1011} ^ 1101 not equals 0

{0111} ^ 1101 not equals 0

As we can we achieved only one 0 therefore the count is 1.

## Approach used in the below program is as follows

• Input two binary strings let's say str_1 and str_2 and pass them to the function cyclic_permutation() for further processing.
• Create a temporary variable to store the result and set str_2 as str_2 + str_2 and then set str_2 as str_2.substr(0, str_2.size()-1).
• Create a string type variable str and set it to the combination of str_1 and str_2 then calculate the length of the string str. Create an array of the length of string str.
• Call the function check() by passing string str and array to the function as an argument.
• Inside the function
• Declare two variables start and end and set them to 0
• Calculate the length of the string.
• Start loop FOR from i till length for string -1 and check IF i greater than end then set start as i and end as i. Now start While end less than length of string AND str[end-start] equals str[end] and increment the value of end by 1
• Now set arr[i] as end - start and decrement the end by 1
• Else, create a temporary variable temp and set it as i - start and check IF arr[temp] less than end - i + 1 then set arr[i] as arr[temp]. Else, set start to i and start WHILE end less than length of a string AND str[end-start] as str[end] then increment the end by 1 and set arr[i] as end - start and decrement the end by 1.
• Start loop FOR from i to 1 till then length of string str -1 and check IF arr[i] equals length of string str_1 then increment the count by 1
• Return count
• Print the result

## Example

Live Demo

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

void check(string str, int arr[]) {
int start = 0, end = 0;
int len = str.length();

for (int i = 1; i <= len - 1; i++) {
if (i > end) {
start = i;
end = i;
while (end < len && str[end - start] == str[end]) {
end++;
}
arr[i] = end - start;
end--;
} else {
int temp = i - start;
if (arr[temp] < end - i + 1) {
arr[i] = arr[temp];
} else {
start = i;
while (end < len && str[end - start] == str[end]) {
end++;
}
arr[i] = end - start;
end--;
}
}
}
}

int cyclic_permutation(string str_1, string str_2) {
int count = 0;
str_2 = str_2 + str_2;
str_2 = str_2.substr(0, str_2.size() - 1);

string str = str_1 + "\$" + str_2;
int len = str.length();
int arr[len];
check(str, arr);

for (int i = 1; i <= len - 1; i++) {
if (arr[i] == str_1.length()) {
count++;
}
}
return count;
}
int main() {
string str_1 = "1111";
string str_2 = "1111";
cout << "Count of cyclic permutations having XOR with other binary string as 0 are: " << cyclic_permutation(str_1, str_2);
return 0;
}

If we run the above code it will generate the following output −

## Output

Count of cyclic permutations having XOR with other binary string as 0 are: 4