Check if a binary string contains consecutive same or not | Java implementation of the approach ; 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
class gfg { static boolean isValid ( String str , int len ) { for ( int i = 1 ; i < len ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( i - 1 ) ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = "0110" ; int len = str . length ( ) ; if ( isValid ( str , len ) ) System . out . println ( " Valid " ) ; else System . out . println ( " Invalid " ) ; } }