Check if it is possible to rearrange a binary string with alternate 0s and 1s
Last Updated :
06 Dec, 2022
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 are alternate 0s and 1s. If possible, then the output is YES, otherwise the 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 an even position and all 1’s in an odd position or we can place all 0’s in an odd position and all 1’s in an 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 in the count
of 1’s and 0’s must be one.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool is_possible(string s)
{
int l = s.length();
int one = 0, zero = 0;
for ( int i = 0; i < l; i++) {
if (s[i] == '0' )
zero++;
else
one++;
}
if (l % 2 == 0)
return (one == zero);
else
return ( abs (one - zero) == 1);
}
int main()
{
string s = "100110" ;
if (is_possible(s))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.lang.Math;
public class GfG{
public static boolean is_possible(String s)
{
int l = s.length();
int one = 0 , zero = 0 ;
for ( int i = 0 ; i < l; i++) {
if (s.charAt(i) == '0' )
zero++;
else
one++;
}
if (l % 2 == 0 )
return (one == zero);
else
return (Math.abs(one - zero) == 1 );
}
public static void main(String []args){
String s = "100110" ;
if (is_possible(s))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python 3
def is_possible(s):
l = len (s)
one = 0
zero = 0
for i in range ( 0 ,l) :
if (s[i] = = '0' ):
zero + = 1
else :
one + = 1
if (l % 2 = = 0 ) :
return (one = = zero)
else :
return ( abs (one - zero) = = 1 )
if __name__ = = "__main__" :
s = "100110"
if (is_possible(s)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GfG
{
public static bool is_possible(String s)
{
int l = s.Length;
int one = 0, zero = 0;
for ( int i = 0; i < l; i++)
{
if (s[i] == '0' )
zero++;
else
one++;
}
if (l % 2 == 0)
return (one == zero);
else
return (Math.Abs(one - zero) == 1);
}
public static void Main(String []args)
{
String s = "100110" ;
if (is_possible(s))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function is_possible( $s )
{
$l = strlen ( $s );
$one = 0;
$zero = 0;
for ( $i = 0; $i < $l ; $i ++)
{
if ( $s [ $i ] == '0' )
$zero ++;
else
$one ++;
}
if ( $l % 2 == 0)
return ( $one == $zero );
else
return ( abs ( $one - $zero ) == 1);
}
$s = "100110" ;
if (is_possible( $s ))
echo ( "Yes" );
else
echo ( "No" );
?>
|
Javascript
<script>
function is_possible(s)
{
let l = s.length;
let one = 0, zero = 0;
for (let i = 0; i < l; i++) {
if (s[i] == '0 ')
zero++;
// count one' s
else
one++;
}
if (l % 2 == 0)
return (one == zero);
else
return (Math.abs(one - zero) == 1);
}
let s = "100110" ;
if (is_possible(s))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(l), where l is the length of the binary string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...