Given a binary string of length at-least two. We need to check if it is possible to rearrange a binary string such that there alternate 0’s and 1’s. If possible then output is YES, otherwise output is NO.

**Examples**:

Input: 1011

Output: NO

We can’t rearrange the string such that it has alternate 0s and 1s.

Input: 1100

Output: YES

There are exactly two ways to rearrange the string, and they are 0101 or 1010 .

We can place all 0’s in even position and all 1’s in odd position or we can place all 0’s in odd position and all 1’s in even position. If the length of the string is even then to satisfy the given condition the count of 1’s and 0’s must be equal. If the length of the string is odd then to satisfy the given condition the absolute difference of count

of 1’s and 0’s must be one.

Below is the implementation of above approach:

## C++

`// CPP program to check if we can rearrange a ` `// string such that it has alternate 0s and 1s. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to check the binary string ` `bool` `is_possible(string s) ` `{ ` ` ` `// length of string ` ` ` `int` `l = s.length(); ` ` ` ` ` `int` `one = 0, zero = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < l; i++) { ` ` ` ` ` `// count zero's ` ` ` `if` `(s[i] == ` `'0'` `) ` ` ` `zero++; ` ` ` ` ` `// count one's ` ` ` `else` ` ` `one++; ` ` ` `} ` ` ` ` ` `// if length is even ` ` ` `if` `(l % 2 == 0) ` ` ` `return` `(one == zero); ` ` ` ` ` `// if length is odd ` ` ` `else` ` ` `return` `(` `abs` `(one - zero) == 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"100110"` `; ` ` ` `if` `(is_possible(s)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` `return` `0; ` `} ` |

## Python 3

`# Python3 program to check if ` `# we can rearrange a ` `# string such that it has alternate ` `# 0s and 1s. ` ` ` `# function to check the binary string ` `def` `is_possible(s): ` ` ` ` ` `# length of string ` ` ` `l ` `=` `len` `(s) ` ` ` ` ` `one ` `=` `0` ` ` `zero ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `0` `,l) : ` ` ` ` ` `# count zero's ` ` ` `if` `(s[i] ` `=` `=` `'0'` `): ` ` ` `zero ` `+` `=` `1` ` ` ` ` `# count one's ` ` ` `else` `: ` ` ` `one ` `+` `=` `1` ` ` ` ` `# if length is even ` ` ` `if` `(l ` `%` `2` `=` `=` `0` `) : ` ` ` `return` `(one ` `=` `=` `zero) ` ` ` ` ` `# if length is odd ` ` ` `else` `: ` ` ` `return` `(` `abs` `(one ` `-` `zero) ` `=` `=` `1` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `s ` `=` `"100110"` ` ` `if` `(is_possible(s)): ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by ` `# ChitraNayal ` |

**Output:**

Yes

**Time Complexity: O(l), where l is length of the binary string.**

## Recommended Posts:

- Rearrange a binary string as alternate x and y occurrences
- Number of flips to make binary string alternate
- Minimum number of characters to be removed to make a binary string alternate
- Python | Check if a given string is binary string or not
- Check divisibility of binary string by 2^k
- Check if a binary string contains all permutations of length k
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if an encoding represents a unique binary string
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Rearrange a string so that all same characters become d distance away
- Rearrange characters in a string such that no two adjacent are same
- Rearrange a string in sorted order followed by the integer sum
- Rearrange a string so that all same characters become atleast d distance away
- Alternate vowel and consonant string

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.