Length of the longest substring with equal 1 s and 0 s | Function to check if a string contains equal number of one and zeros or not ; Function to find the length of the longest balanced substring ; Driver code ; Function call
function isValid ( p ) { var n = p . length ; var c1 = 0 , c0 = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( p [ i ] == ' ' ) c0 ++ ; if ( p [ i ] == ' ' ) c1 ++ ; } return ( c0 == c1 ) ? true : false ; } function longestSub ( s ) { var max_len = 0 ; var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i ; j < n ; j ++ ) { if ( isValid ( s . substr ( i , j - i + 1 ) ) && max_len < j - i + 1 ) max_len = j - i + 1 ; } } return max_len ; } var s = " " ; document . write ( longestSub ( s ) ) ;