Check if given Binary string follows then given condition or not | C ++ program for the above approach ; Function to check if the string follows rules or not ; Check for the first condition ; Check for the third condition ; Check for the second condition ; Driver Code ; Given String str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkrules ( string s ) { if ( s . length ( ) == 0 ) return true ; if ( s [ 0 ] != '1' ) return false ; if ( s . length ( ) > 2 ) { if ( s [ 1 ] == '0' && s [ 2 ] == '0' ) return checkrules ( s . substr ( 3 ) ) ; } return checkrules ( s . substr ( 1 ) ) ; } int main ( ) { string str = "1111" ; if ( checkrules ( str ) ) { cout << " Valid ▁ String " ; } else { cout << " Invalid ▁ String " ; } return 0 ; }