Check if it is possible to rearrange a binary string with alternate 0 s and 1 s | function to check the binary string ; length of string ; count zero 's ; count one 's ; if length is even ; if length is odd ; Driver code
< ? 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 " ) ; ? >