Check if a binary string has a 0 between 1 s or not | Set 1 ( General approach ) | Function returns 1 when string is valid else returns 0 ; Find first occurrence of 1 in s [ ] ; Find last occurrence of 1 in s [ ] ; Check if there is any 0 in range ; Driver code
def checkString ( s ) : NEW_LINE INDENT Len = len ( s ) NEW_LINE first = len ( s ) + 1 NEW_LINE for i in range ( Len ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT first = i NEW_LINE break NEW_LINE DEDENT DEDENT last = 0 NEW_LINE for i in range ( Len - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT last = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( first , last + 1 ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT s = "00011111111100000" NEW_LINE if ( checkString ( s ) ) : NEW_LINE INDENT print ( " VALID " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NOT ▁ VALID " ) NEW_LINE DEDENT