Given a binary string, the task is to find whether all the digits of the string can be made equal i.e either 0 or 1 by flipping two consecutive bits any number of times.

**Examples:**

Input:01011Output:YESExplanation:Flip 2nd and 3rd bit -> 00111, again flipping 1'st and 2'nd bit -> 11111Input:100011Output:NOExplanation:No number of moves can ever equalize all elements of the array.

**Approach:**

On careful observation, toggling of i’th and j’th bit can be done by toggling from i’th bit like (i, i+1), (i+1, i+2) …. (j-1, j) here every bit is toggling twice (if bit is toggle twice then its come to its initial value) except i and j then ultimately i’th and j’th bits toggle. Therefore, it can be said that it is only not possible to make all digits in binary string equal when the count of both 1 and 0 is odd.

Below is the implementation of the above approach:

## C++

`// C++ program for the ` `// above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if ` `// Binary string can be ` `// made equal ` `string canMake(string& s) ` `{ ` ` ` ` ` `int` `o = 0, z = 0; ` ` ` ` ` `// Counting occurence of ` ` ` `// zero and one in binary ` ` ` `// string ` ` ` `for` `(` `int` `i = 0; i < s.size(); i++) { ` ` ` `if` `(s[i] - ` `'0'` `== 1) ` ` ` `o++; ` ` ` `else` ` ` `z++; ` ` ` `} ` ` ` ` ` `// From above observation ` ` ` `if` `(o % 2 == 1 && z % 2 == 1) ` ` ` `return` `"NO"` `; ` ` ` `else` ` ` `return` `"YES"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `string s = ` `"01011"` `; ` ` ` `cout << canMake(s) << ` `'\n'` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to check if ` ` ` `// Binary string can be ` ` ` `// made equal ` ` ` `static` `String canMake(String s) ` ` ` `{ ` ` ` ` ` `int` `o = ` `0` `, z = ` `0` `; ` ` ` ` ` `// Counting occurence of ` ` ` `// zero and one in binary ` ` ` `// string ` ` ` `for` `(` `int` `i = ` `0` `; i < s.length(); i++) ` ` ` `{ ` ` ` `if` `(s.charAt(i) - ` `'0'` `== ` `1` `) ` ` ` `o++; ` ` ` `else` ` ` `z++; ` ` ` `} ` ` ` ` ` `// From above observation ` ` ` `if` `(o % ` `2` `== ` `1` `&& z % ` `2` `== ` `1` `) ` ` ` `return` `"NO"` `; ` ` ` `else` ` ` `return` `"YES"` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` ` ` `String s = ` `"01011"` `; ` ` ` `System.out.println(canMake(s)) ; ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to check if ` `# Binary string can be ` `# made equal ` `def` `canMake(s) : ` ` ` ` ` `o ` `=` `0` `; z ` `=` `0` `; ` ` ` ` ` `# Counting occurence of ` ` ` `# zero and one in binary ` ` ` `# string ` ` ` `for` `i ` `in` `range` `(` `len` `(s)) : ` ` ` `if` `(` `ord` `(s[i]) ` `-` `ord` `(` `'0'` `) ` `=` `=` `1` `) : ` ` ` `o ` `+` `=` `1` `; ` ` ` `else` `: ` ` ` `z ` `+` `=` `1` `; ` ` ` ` ` `# From above observation ` ` ` `if` `(o ` `%` `2` `=` `=` `1` `and` `z ` `%` `2` `=` `=` `1` `) : ` ` ` `return` `"NO"` `; ` ` ` `else` `: ` ` ` `return` `"YES"` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s ` `=` `"01011"` `; ` ` ` `print` `(canMake(s)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to check if ` ` ` `// Binary string can be ` ` ` `// made equal ` ` ` `static` `string` `canMake(` `string` `s) ` ` ` `{ ` ` ` ` ` `int` `o = 0, z = 0; ` ` ` ` ` `// Counting occurence of ` ` ` `// zero and one in binary ` ` ` `// string ` ` ` `for` `(` `int` `i = 0; i < s.Length; i++) ` ` ` `{ ` ` ` `if` `(s[i] - ` `'0'` `== 1) ` ` ` `o++; ` ` ` `else` ` ` `z++; ` ` ` `} ` ` ` ` ` `// From above observation ` ` ` `if` `(o % 2 == 1 && z % 2 == 1) ` ` ` `return` `"NO"` `; ` ` ` `else` ` ` `return` `"YES"` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `string` `s = ` `"01011"` `; ` ` ` `Console.WriteLine(canMake(s)) ; ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

YES

**Time Complexity:** O(n), where n is the length of the given Binary number

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if all bits can be made same by single flip
- Check if all strings of an array can be made same by interchanging characters
- Maximize distance between any two consecutive 1's after flipping M 0's
- Maximum number of consecutive 1s after flipping all 0s in a K length subarray
- Number formed by flipping all bits to the left of rightmost set bit
- Check if a two character string can be made using given words
- Check whether two strings can be made equal by increasing prefixes
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Check if two strings can be made equal by swapping one character among each other
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Maximize the number by flipping at most K bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Check whether two strings contain same characters in same order
- Check if string can be made lexicographically smaller by reversing any substring
- Check whether the string S1 can be made equal to S2 with the given operation
- Print all possible strings that can be made by placing spaces
- Print all possible strings that can be made by placing spaces
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if a string can be split into two strings with same number of K-frequent characters
- Check if frequency of all characters can become same by one removal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.