Check if all bits can be made same by flipping two consecutive bits | Function to check if Binary string can be made equal ; Counting occurence of zero and one in binary string ; From above observation ; Driver code
function canMake ( s ) { var o = 0 , z = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) == 1 ) o ++ ; else z ++ ; } if ( o % 2 == 1 && z % 2 == 1 ) return " " ; else return " " ; } var s = " " ; document . write ( canMake ( s ) ) ;