Check if a binary string has a 0 between 1 s or not | Set 1 ( General approach ) | Method 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 method
function checkString ( s ) { let len = s . length ; let first = 0 ; for ( let i = 0 ; i < len ; i ++ ) { if ( s [ i ] == ' ' ) { first = i ; break ; } } let last = 0 ; for ( let i = len - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) { last = i ; break ; } } for ( let i = first ; i <= last ; i ++ ) if ( s [ i ] == ' ' ) return false ; return true ; } let s = " " ; document . write ( checkString ( s ) ? " " : " " ) ;