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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( string str , int len ) { for ( int i = 1 ; i < len ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) return false ; } return true ; } int main ( ) { string str = "0110" ; int len = str . length ( ) ; if ( isValid ( str , len ) ) cout << " Valid " ; else cout << " Invalid " ; return 0 ; }