Length of the longest substring with equal 1 s and 0 s | C # program to find the length of the longest balanced substring ; 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
using System ; class GFG { static bool isValid ( string p ) { int n = p . Length ; int c1 = 0 , c0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] == '0' ) { c0 ++ ; } if ( p [ i ] == '1' ) { c1 ++ ; } } if ( c0 == c1 ) { return true ; } else { return false ; } } public static int longestSub ( string s ) { int max_len = 0 ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( isValid ( s . Substring ( i , j - i + 1 ) ) && max_len < j - i + 1 ) { max_len = j - i + 1 ; } } } return max_len ; } static public void Main ( ) { string s = "101001000" ; Console . WriteLine ( longestSub ( s ) ) ; } }