Check if a binary string contains consecutive same or not | Function that returns true is str is valid ; Assuming the string is binary If any two consecutive characters are equal then the string is invalid ; If the string is alternating ; Driver code
< ? php function isValid ( $ str , $ len ) { for ( $ i = 1 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == $ str [ $ i - 1 ] ) return false ; } return true ; } $ str = "0110" ; $ len = strlen ( $ str ) ; if ( isValid ( $ str , $ len ) ) echo " Valid " ; else echo " Invalid " ; ? >