Check if a binary string contains consecutive same or not | C # 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
using System ; class gfg { static bool isValid ( string str , int len ) { for ( int i = 1 ; i < len ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) return false ; } return true ; } public static void Main ( ) { string str = "0110" ; int len = str . Length ; if ( isValid ( str , len ) ) Console . Write ( " Valid " ) ; else Console . Write ( " Invalid " ) ; } }