Split a Binary String such that count of 0 s and 1 s in left and right substrings is maximum | Java program to implement the above approach ; Function to find the maximum sum of count of 0 s in the left subString and count of 1 s in the right subString by splitting the String ; Stores count of 1 s the in binary String ; Traverse the binary String ; If current character is '1' ; Update cntOne ; Stores count of 0 s ; Stores count of 1 s ; Stores maximum sum of count of 0 s and 1 s by splitting the String ; Traverse the binary String ; If current character is '0' ; Update zero ; If current character is '1' ; Update one ; Update res ; Driver Code
import java . util . * ; class GFG { static int maxSumbySplittingString ( String str , int N ) { int cntOne = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == '1' ) { cntOne ++ ; } } int zero = 0 ; int one = 0 ; int res = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( str . charAt ( i ) == '0' ) { zero ++ ; } else { one ++ ; } res = Math . max ( res , zero + cntOne - one ) ; } return res ; } public static void main ( String [ ] args ) { String str = "00111" ; int N = str . length ( ) ; System . out . print ( maxSumbySplittingString ( str , N ) ) ; } }