Check if a binary string has two consecutive occurrences of one everywhere | C ++ implementation of the approach ; Function that returns 1 if str is valid ; Index of first appearance of ' b ' ; If str starts with ' b ' ; While ' b ' occurs in str ; If ' b ' doesn ' t ▁ appear ▁ after ▁ an ▁ ' a ' ; If ' b ' is not succeeded by another ' b ' ; If sub - string is of the type " abbb " ; If str ends with a single b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValidString ( string str , int n ) { int index = find ( str . begin ( ) , str . end ( ) , ' b ' ) - str . begin ( ) ; if ( index == 0 ) return false ; while ( index <= n - 1 ) { if ( str [ index - 1 ] != ' a ' ) return false ; if ( index + 1 < n && str [ index + 1 ] != ' b ' ) return false ; if ( index + 2 < n && str [ index + 2 ] == ' b ' ) return false ; if ( index == n - 1 ) return false ; index = find ( str . begin ( ) + index + 2 , str . end ( ) , ' b ' ) - str . begin ( ) ; } return true ; } int main ( ) { string str = " abbaaabbabba " ; int n = str . length ( ) ; isValidString ( str , n ) ? cout << " true " : cout << " false " ; return 0 ; }