Check if a binary string has two consecutive occurrences of one everywhere | 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
function isValidString ( str , n ) { let index = str . indexOf ( " " ) ; if ( index == 0 ) return false ; while ( index != - 1 ) { if ( str [ index - 1 ] != ' ' ) return false ; if ( index + 1 < n && str [ index + 1 ] != ' ' ) return false ; if ( index + 2 < n && str [ index + 2 ] == ' ' ) return false ; if ( index == n - 1 ) return false ; index = str . indexOf ( " " , index + 2 ) ; } return true ; } let str = " " ; let n = str . length ; document . write ( isValidString ( str , n ) ) ;