text: Count ways to split a Binary String into three substrings having equal count of zeros | C # implementation for the above approach ; Function to return ways to split a string into three parts with the equal number of 0 ; Store total count of 0 s ; Count total no . of 0 s character in given string ; If total count of 0 character is not divisible by 3 ; Initialize map to store frequency of k ; Traverse string to find ways to split string ; Increment count if 0 appears ; Increment result if sum equal to 2 * k and k exists in map ; Insert sum in map ; Return result ; Driver Code ; Given string ; Function call
code: using System ; using System . Collections . Generic ; class GFG { static int count ( String s ) { int cnt = 0 ; foreach ( char c in s . ToCharArray ( ) ) { cnt += c == '0' ? 1 : 0 ; } if ( cnt % 3 != 0 ) return 0 ; int res = 0 , k = cnt / 3 , sum = 0 ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { sum += s [ i ] == '0' ? 1 : 0 ; if ( sum == 2 * k && map . ContainsKey ( k ) && i < s . Length - 1 && i > 0 ) { res += map [ k ] ; } if ( map . ContainsKey ( sum ) ) map [ sum ] = map [ sum ] + 1 ; else map . Add ( sum , 1 ) ; } return res ; } public static void Main ( String [ ] args ) { String str = "01010" ; Console . WriteLine ( count ( str ) ) ; } }