text: Count ways to split a Binary String into three substrings having equal count of zeros | Java 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: import java . util . * ; import java . lang . * ; class GFG { static int count ( String s ) { int cnt = 0 ; for ( char c : s . toCharArray ( ) ) { cnt += c == '0' ? 1 : 0 ; } if ( cnt % 3 != 0 ) return 0 ; int res = 0 , k = cnt / 3 , sum = 0 ; Map < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum += s . charAt ( i ) == '0' ? 1 : 0 ; if ( sum == 2 * k && map . containsKey ( k ) && i < s . length ( ) - 1 && i > 0 ) { res += map . get ( k ) ; } map . put ( sum , map . getOrDefault ( sum , 0 ) + 1 ) ; } return res ; } public static void main ( String [ ] args ) { String str = "01010" ; System . out . println ( count ( str ) ) ; } }