codeparrot/xlcost-text-to-code huggingface dataset:
Program to count vowels , consonant , digits and special characters in string . | Program to count vowels , consonant , digits and special character in a given string ; Function to count number of vowels , consonant , digitsand special character in a string . ; Declare the variable vowels , consonant , digit and special characters ; str . length ( ) function to count number of character in given string . ; To handle upper case letters ; Driver function .
using System ; using System . Globalization ; class GFG { static void countCharacterType ( string str ) { int vowels = 0 , consonant = 0 , specialChar = 0 , digit = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { char ch = str [ i ] ; if ( ( ch >= ' a ' && ch <= ' z ' ) || ( ch >= ' A ' && ch <= ' Z ' ) ) { ch = char . ToLower ( ch ) ; if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) vowels ++ ; else consonant ++ ; } else if ( ch >= '0' && ch <= '9' ) digit ++ ; else specialChar ++ ; } Console . WriteLine ( " Vowels : ▁ " + vowels ) ; Console . WriteLine ( " Consonant : ▁ " + consonant ) ; Console . WriteLine ( " Digit : ▁ " + digit ) ; Console . WriteLine ( " Special ▁ Character : ▁ " + specialChar ) ; } static public void Main ( ) { string str = " geeks ▁ for ▁ geeks121" ; countCharacterType ( str ) ; } }
Next word that does not contain a palindrome and has characters from first k | C # program to find lexicographically next word which contains first K letters of the English alphabet and does not contain a palindrome as it 's substring of length more than one. ; function to return lexicographically next word ; we made m as m + 97 that means our required string contains not more than m + 97 ( as per ASCII value ) in it . ; increment last alphabet to make next lexicographically next word . ; if i - th alphabet not in first k letters then make it as " a " and then increase ( i - 1 ) th letter ; to check whether formed string palindrome or not . ; increment i . ; if i less than or equals to one that means we not formed such word . ; Driver code
using System ; class GFG { static void findNextWord ( char [ ] s , int m ) { m += 97 ; int n = s . Length ; int i = s . Length - 1 ; s [ i ] ++ ; while ( i >= 0 && i <= n - 1 ) { if ( s [ i ] >= m ) { s [ i ] = ' a ' ; s [ -- i ] ++ ; } else if ( s [ i ] == s [ i - 1 ] s [ i ] == s [ i - 2 ] ) { s [ i ] ++ ; } else { i ++ ; } } if ( i <= - 1 ) { Console . WriteLine ( " - 1" ) ; } else { Console . WriteLine ( s ) ; } } public static void Main ( String [ ] args ) { char [ ] str = " abcd " . ToCharArray ( ) ; int k = 4 ; findNextWord ( str , k ) ; } }
Program to implement ASCII lookup table | C # implementation for ASCII table lookup ; Function to convert decimal value to equivalent octal value ; Function to convert decimal value to equivalent hexadecimal value ; Function to convert decimal value to equivalent HTML value ; ASCII lookup table ; Implicit typecasting converts the character into it 's equivalent ASCII ; Driver Code
using System ; class GeeksforGeeks { static int Octal ( int decima ) { int octal = 0 ; String temp = " " ; while ( decima > 0 ) { int remainder = decima % 8 ; temp = remainder + temp ; decima /= 8 ; } for ( int i = 0 ; i < temp . Length ; i ++ ) octal = ( octal * 10 ) + ( temp [ i ] - '0' ) ; return octal ; } static String Hexadecimal ( int decima ) { String hex = " " ; while ( decima > 0 ) { int remainder = decima % 16 ; if ( remainder >= 0 && remainder <= 9 ) hex = remainder + hex ; else hex = ( char ) ( ' A ' + remainder % 10 ) + hex ; decima /= 16 ; } return hex ; } static String HTML ( int decima ) { String html = " " ; html = html + decima ; html = " & # " + html + " ; " ; return html ; } static void ASCIIlookuptable ( char ch ) { int decima = ch ; Console . WriteLine ( " Octal ▁ value : ▁ " + Octal ( decima ) ) ; Console . WriteLine ( " Decimal ▁ value : ▁ " + decima ) ; Console . WriteLine ( " Hexadecimal ▁ value : ▁ " + Hexadecimal ( decima ) ) ; Console . Write ( " HTML ▁ value : ▁ " + HTML ( decima ) ) ; } public static void Main ( ) { char ch = ' @ ' ; ASCIIlookuptable ( ch ) ; } }
Replace a character c1 with c2 and c2 with c1 in a string S | C # program to replace c1 with c2 and c2 with c1 ; loop to traverse in the string ; check for c1 and replace ; check for c2 and replace ; Driver code
using System ; public class GFG { static String replace ( String s , char c1 , char c2 ) { int l = s . Length ; char [ ] arr = s . ToCharArray ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( arr [ i ] == c1 ) arr [ i ] = c2 ; else if ( arr [ i ] == c2 ) arr [ i ] = c1 ; } return string . Join ( " " , arr ) ; } public static void Main ( ) { String s = " grrksfoegrrks " ; char c1 = ' e ' , c2 = ' r ' ; Console . WriteLine ( replace ( s , c1 , c2 ) ) ; } }
Fibonacci Word | C # program for nth Fibonacci word ; Returns n - th Fibonacci word ; Driver code
using System ; class GFG { public static String fibWord ( int n ) { String Sn_1 = "0" ; String Sn = "01" ; String tmp ; for ( int i = 2 ; i <= n ; i ++ ) { tmp = Sn ; Sn += Sn_1 ; Sn_1 = tmp ; } return Sn ; } public static void Main ( ) { int n = 6 ; Console . WriteLine ( fibWord ( n ) ) ; } }
Decimal to octal conversion with minimum use of arithmetic operators | C # implementation of decimal to octal conversion with minimum use of arithmetic operators ; Function for decimal to binary conversion without using arithmetic operators ; To store the binary equivalent of decimal ; To get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; Right shift ' n ' by 1 ; Required binary number ; Function to find octal equivalent of binary ; Add min 0 's in the beginning to make  string length divisible by 3 ; Create map between binary and its equivalent octal code ; One by one extract from left , substring of size 3 and add its octal code ; Required octal number ; Function to find octal equivalent of decimal ; Convert decimal to binary ; Convert binary to octal required octal equivalent of decimal ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static string decToBin ( int n ) { if ( n == 0 ) return "0" ; string bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } static string convertBinToOct ( string bin ) { int l = bin . Length ; for ( int j = 1 ; j <= ( 3 - l % 3 ) % 3 ; j ++ ) bin = '0' + bin ; Dictionary < string , char > bin_oct_map = new Dictionary < string , char > ( ) ; bin_oct_map . Add ( "000" , '0' ) ; bin_oct_map . Add ( "001" , '1' ) ; bin_oct_map . Add ( "010" , '2' ) ; bin_oct_map . Add ( "011" , '3' ) ; bin_oct_map . Add ( "100" , '4' ) ; bin_oct_map . Add ( "101" , '5' ) ; bin_oct_map . Add ( "110" , '6' ) ; bin_oct_map . Add ( "111" , '7' ) ; int i = 0 ; string octal = " " ; while ( true ) { octal += bin_oct_map [ bin . Substring ( i , 3 ) ] ; i += 3 ; if ( i == bin . Length ) break ; } return octal ; } static string decToOctal ( int n ) { string bin = decToBin ( n ) ; return convertBinToOct ( bin ) ; } public static void Main ( string [ ] args ) { int n = 151 ; Console . Write ( decToOctal ( n ) ) ; } }
Split the string into substrings using delimiter | C # implementation to split String into substrings on the basis of delimiter ; function to split String into subStrings on the basis of delimiter and return the subStrings after split ; to count the number of split Strings ; adding delimiter character at the end of ' str ' ; length of ' str ' ; traversing ' str ' from left to right ; if str [ i ] is not equal to the delimiter character then accumulate it to ' word ' ; if ' word ' is not an empty String , then add this ' word ' to the array ' substr _ list [ ] ' ; reset ' word ' ; return the splitted Strings ; Driver code
using System ; using System . Collections . Generic ; class GFG { static List < String > splitStrings ( String str , char dl ) { String word = " " ; int num = 0 ; str = str + dl ; int l = str . Length ; List < String > substr_list = new List < String > ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( str [ i ] != dl ) { word = word + str [ i ] ; } else { if ( ( int ) word . Length != 0 ) { substr_list . Add ( word ) ; } word = " " ; } } return substr_list ; } public static void Main ( ) { String str = " geeks ; for ; geeks " ; char dl = ' ; ' ; List < String > res = splitStrings ( str , dl ) ; foreach ( String x in res ) { Console . WriteLine ( x ) ; } } }
Construct lexicographically smallest palindrome | C # for constructing smallest palindrome ; function for printing palindrome ; iterate till i < j ; continue if str [ i ] == str [ j ] ; update str [ i ] = str [ j ] = ' a ' if both are ' * ' ; update str [ i ] = str [ j ] if only str [ i ] = ' * ' ; update str [ j ] = str [ i ] if only str [ j ] = ' * ' ; else print not possible and return ; Driver code
using System ; class GFG { static String constructPalin ( char [ ] str , int len ) { int i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != ' * ' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == ' * ' ) { str [ i ] = ' a ' ; str [ j ] = ' a ' ; continue ; } else if ( str [ i ] == ' * ' ) { str [ i ] = str [ j ] ; continue ; } else if ( str [ j ] == ' * ' ) { str [ j ] = str [ i ] ; continue ; } Console . WriteLine ( " Not ▁ Possible " ) ; return " " ; } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " bca * xc * * b " ; int len = str . Length ; Console . WriteLine ( constructPalin ( str . ToCharArray ( ) , len ) ) ; } }
Change string to a new character set | C # program to change the sentence with virtual dictionary ; Converts str to given character set ; hashing for new character set ; conversion of new character set ; Driver code
using System ; class GFG { static String conversion ( char [ ] charSet , String str ) { int n = str . Length ; char [ ] hashChar = new char [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { int ch = Math . Abs ( charSet [ i ] - ' a ' ) ; hashChar [ ch ] = ( char ) ( ' a ' + i ) ; } String s = " " ; for ( int i = 0 ; i < n ; i ++ ) { s += hashChar [ str [ i ] - ' a ' ] ; } return s ; } public static void Main ( String [ ] args ) { char [ ] charSet = " qwertyuiopasdfghjklzxcvbnm " . ToCharArray ( ) ; String str = " egrt " ; str = conversion ( charSet , str ) ; Console . WriteLine ( str ) ; } }
Change string to a new character set | C # program to change the sentence with virtual dictionary ; function for converting the string ; find the index of each element of the string in the modified set of alphabets replace the element with the one having the same index in the actual set of alphabets ; Driver Code
using System ; class GFG { static char [ ] alphabets = " abcdefghijklmnopqrstuvwxyz " . ToCharArray ( ) ; static String conversion ( String charSet , char [ ] str1 ) { String s2 = " " ; foreach ( char i in str1 ) s2 += alphabets [ charSet . IndexOf ( i ) ] ; return s2 ; } public static void Main ( String [ ] args ) { String charSet = " qwertyuiopasdfghjklzxcvbnm " ; String str1 = " egrt " ; Console . Write ( conversion ( charSet , str1 . ToCharArray ( ) ) ) ; } }
Different substrings in a string that start and end with given strings | C # program to find number of different sub strings ; function to return number of different sub - strings ; initially our answer is zero . ; find the length of given strings ; currently make array and initially put zero . ; find occurrence of " a " and " b " in string " s " ; We use a hash to make sure that same substring is not counted twice . ; go through all the positions to find occurrence of " a " first . ; if we found occurrence of " a " . ; then go through all the positions to find occurrence of " b " . ; if we do found " b " at index j then add it to already existed substring . ; if we found occurrence of " b " . ; now add string " b " to already existed substring . ; If current substring is not included already . ; put any non negative integer to make this string as already existed . ; make substring null . ; return answer . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int numberOfDifferentSubstrings ( String s , char a , char b ) { int ans = 0 ; int ls = s . Length ; int [ ] x = new int [ ls ] ; int [ ] y = new int [ ls ] ; for ( int i = 0 ; i < ls ; i ++ ) { if ( s [ i ] == a ) x [ i ] = 1 ; if ( s [ i ] == b ) y [ i ] = 1 ; } HashSet < String > hash = new HashSet < String > ( ) ; String curr_substr = " " ; for ( int i = 0 ; i < ls ; i ++ ) { if ( x [ i ] != 0 ) { for ( int j = i ; j < ls ; j ++ ) { if ( y [ j ] == 0 ) curr_substr += s [ i ] ; if ( y [ j ] != 0 ) { curr_substr += s [ j ] ; if ( ! hash . Contains ( curr_substr ) ) ans ++ ; hash . Add ( curr_substr ) ; } } curr_substr = " " ; } } return ans ; } public static void Main ( String [ ] args ) { String s = " codecppforfood " ; char begin = ' c ' ; char end = ' d ' ; Console . WriteLine ( numberOfDifferentSubstrings ( s , begin , end ) ) ; } }
Printing string in plus ‘ + ’ pattern in the matrix | C # program to print the string in ' plus ' pattern ; Function to make a cross in the matrix ; As , it is not possible to make the cross exactly in the middle of the matrix with an even length string . ; declaring a 2D array i . e a matrix ; Now , we will fill all the elements of the array with ' X ' ; Now , we will place the characters of the string in the matrix , such that a cross is formed in it . ; here the characters of the string will be added in the middle column of our array . ; here the characters of the string will be added in the middle row of our array . ; Now finally , we will print the array ; Driver code
using System ; class GFG { static int max = 100 ; static void carveCross ( String str ) { int n = str . Length ; if ( n % 2 == 0 ) { Console . Write ( " Not ▁ possible . ▁ Please ▁ enter ▁ " + " odd ▁ length ▁ string . " ) ; } else { char [ , ] arr = new char [ max , max ] ; int m = n / 2 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { arr [ i , j ] = ' X ' ; } } for ( int i = 0 ; i < n ; i ++ ) { arr [ i , m ] = str [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { arr [ m , i ] = str [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( arr [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } } public static void Main ( ) { string str = " PICTURE " ; carveCross ( str ) ; } }
Binary String of given length that without a palindrome of size 3 | C # program find a binary String of given length that doesn 't contain a palindrome of size 3. ; Printing the character according to i ; Driver code
using System ; class GFG { static void generateString ( int n ) { String s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += ( ( i & 2 ) > 1 ? ' b ' : ' a ' ) ; Console . WriteLine ( s ) ; } public static void Main ( String [ ] args ) { int n = 5 ; generateString ( n ) ; n = 8 ; generateString ( n ) ; n = 10 ; generateString ( n ) ; } }
Program to find remainder when large number is divided by 11 | C # implementation to find remainder when a large number is divided by 11 ; Function to return remainder ; len is variable to store the length of number string . ; loop that find remainder ; Driver code
using System ; class GFG { static int remainder ( string str ) { int len = str . Length ; int num , rem = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = rem * 10 + ( str [ i ] - '0' ) ; rem = num % 11 ; } return rem ; } public static void Main ( ) { string str = "3435346456547566345436457867978" ; Console . WriteLine ( remainder ( str ) ) ; } }
Longest subsequence of the form 0 * 1 * 0 * in a binary string | C # program to find longest subsequence of the form 0 * 1 * 0 * in a binary string ; Returns length of the longest subsequence of the form 0 * 1 * 0 * ; Precomputing values in three arrays pre_count_0 [ i ] is going to store count of 0 s in prefix str [ 0. . i - 1 ] pre_count_1 [ i ] is going to store count of 1 s in prefix str [ 0. . i - 1 ] post_count_0 [ i ] is going to store count of 0 s in suffix str [ i - 1. . n - 1 ] ; string is made up of all 0 s or all 1 s ; Compute result using precomputed values ; Driver code
using System ; class GFG { public static int longestSubseq ( String s ) { int n = s . Length ; int [ ] pre_count_0 = new int [ n + 2 ] ; int [ ] pre_count_1 = new int [ n + 1 ] ; int [ ] post_count_0 = new int [ n + 2 ] ; pre_count_0 [ 0 ] = 0 ; post_count_0 [ n + 1 ] = 0 ; pre_count_1 [ 0 ] = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { pre_count_0 [ j ] = pre_count_0 [ j - 1 ] ; pre_count_1 [ j ] = pre_count_1 [ j - 1 ] ; post_count_0 [ n - j + 1 ] = post_count_0 [ n - j + 2 ] ; if ( s [ j - 1 ] == '0' ) pre_count_0 [ j ] ++ ; else pre_count_1 [ j ] ++ ; if ( s [ n - j ] == '0' ) post_count_0 [ n - j + 1 ] ++ ; } if ( pre_count_0 [ n ] == n pre_count_0 [ n ] == 0 ) return n ; int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) ans = Math . Max ( pre_count_0 [ i - 1 ] + pre_count_1 [ j ] - pre_count_1 [ i - 1 ] + post_count_0 [ j + 1 ] , ans ) ; return ans ; } public static void Main ( String [ ] args ) { String s = "000011100000" ; Console . WriteLine ( longestSubseq ( s ) ) ; } }
Distinct permutations of the string | Set 2 | C # program to distinct permutations of the string ; Returns true if str [ curr ] does not matches with any of the characters after str [ start ] ; Prints all distinct permutations in str [ 0. . n - 1 ] ; Proceed further for str [ i ] only if it doesn 't match with any of  the characters after str[index] ; Driver code
using System ; class GFG { static bool shouldSwap ( char [ ] str , int start , int curr ) { for ( int i = start ; i < curr ; i ++ ) { if ( str [ i ] == str [ curr ] ) { return false ; } } return true ; } static void findPermutations ( char [ ] str , int index , int n ) { if ( index >= n ) { Console . WriteLine ( str ) ; return ; } for ( int i = index ; i < n ; i ++ ) { bool check = shouldSwap ( str , index , i ) ; if ( check ) { swap ( str , index , i ) ; findPermutations ( str , index + 1 , n ) ; swap ( str , index , i ) ; } } } static void swap ( char [ ] str , int i , int j ) { char c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; } public static void Main ( ) { char [ ] str = { ' A ' , ' B ' , ' C ' , ' A ' } ; int n = str . Length ; findPermutations ( str , 0 , n ) ; } }
Generate permutations with only adjacent swaps allowed | C # program to generate permutations with only one swap allowed . ; don 't swap the current position ; Swap with the next character and revert the changes . As explained above , swapping with previous is is not needed as it anyways happens for next character . ; Driver code
using System ; public class GFG { static void findPermutations ( char [ ] str , int index , int n ) { if ( index >= n || ( index + 1 ) >= n ) { Console . WriteLine ( str ) ; return ; } findPermutations ( str , index + 1 , n ) ; swap ( str , index ) ; findPermutations ( str , index + 2 , n ) ; swap ( str , index ) ; } static void swap ( char [ ] arr , int index ) { char temp = arr [ index ] ; arr [ index ] = arr [ index + 1 ] ; arr [ index + 1 ] = temp ; } public static void Main ( ) { char [ ] str = "12345" . ToCharArray ( ) ; int n = str . Length ; findPermutations ( str , 0 , n ) ; } }
Decode a median string to the original string | C # program to decode a median string to the original string ; function to calculate the median back string ; length of string ; initialize a blank string ; Flag to check if length is even or odd ; traverse from first to last ; if len is even then add first character to beginning of new string and second character to end ; if current length is odd and is greater than 1 ; add first character to end and second character to beginning ; if length is 1 , add character to end ; Driver code
using System ; class GFG { static string decodeMedianString ( string s ) { int l = s . Length ; string s1 = " " ; bool isEven = ( l % 2 == 0 ) ? true : false ; for ( int i = 0 ; i < l ; i += 2 ) { if ( isEven ) { s1 = s [ i ] + s1 ; s1 += s [ i + 1 ] ; } else { if ( l - i > 1 ) { s1 += s [ i ] ; s1 = s [ i + 1 ] + s1 ; } else { s1 += s [ i ] ; } } } return s1 ; } public static void Main ( ) { string s = " eekgs " ; Console . WriteLine ( decodeMedianString ( s ) ) ; } }
Maximum number of characters between any two same character in a string | Simple C # program to find maximum number of characters between two occurrences of same character ; Driver code
using System ; public class GFG { static int maximumChars ( string str ) { int n = str . Length ; int res = - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( str [ i ] == str [ j ] ) res = Math . Max ( res , Math . Abs ( j - i - 1 ) ) ; return res ; } static public void Main ( ) { string str = " abba " ; Console . WriteLine ( maximumChars ( str ) ) ; } }
Maximum number of characters between any two same character in a string | Efficient C # program to find maximum number of characters between two occurrences of same character ; Initialize all indexes as - 1. ; If this is first occurrence ; Else find distance from previous occurrence and update result ( if required ) . ; Driver code
using System ; public class GFG { static int MAX_CHAR = 256 ; static int maximumChars ( string str ) { int n = str . Length ; int res = - 1 ; int [ ] firstInd = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) firstInd [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int first_ind = firstInd [ str [ i ] ] ; if ( first_ind == - 1 ) firstInd [ str [ i ] ] = i ; else res = Math . Max ( res , Math . Abs ( i - first_ind - 1 ) ) ; } return res ; } static public void Main ( ) { string str = " abba " ; Console . WriteLine ( maximumChars ( str ) ) ; } }
Check if an encoding represents a unique binary string | C # program to check if given encoding represents a single string . ; Return true if sum becomes k ; Driver Code
using System ; class GFG { static bool isUnique ( int [ ] a , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; sum += n - 1 ; return ( sum == k ) ; } static public void Main ( ) { int [ ] a = { 3 , 3 , 3 } ; int n = a . Length ; int k = 12 ; if ( isUnique ( a , n , k ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Lexicographically next string | C # program to find lexicographically next string ; if string is empty ; Find first character from right which is not z . ; If all characters are ' z ' , append an ' a ' at the end . ; If there are some non - z characters ; Driver Code
using System ; class GFG { public static string nextWord ( string str ) { if ( str == " " ) { return " a " ; } int i = str . Length - 1 ; while ( str [ i ] == ' z ' && i >= 0 ) { i -- ; } if ( i == - 1 ) { str = str + ' a ' ; } else { str = str . Substring ( 0 , i ) + ( char ) ( ( int ) ( str [ i ] ) + 1 ) + str . Substring ( i + 1 ) ; } return str ; } public static void Main ( string [ ] args ) { string str = " samez " ; Console . Write ( nextWord ( str ) ) ; } }
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 ) ) ; } }