Count ways to split a Binary String into three substrings having equal count of zeros | 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 mp 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 mp ; Insert sum in mp ; Return result ; Given string ; Function call
function count ( s ) { var cnt = 0 ; s . split ( ' ' ) . forEach ( c => { cnt += ( c == ' ' ) ? 1 : 0 ; } ) ; if ( cnt % 3 != 0 ) return 0 ; var res = 0 , k = parseInt ( cnt / 3 ) , sum = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) { sum += ( s [ i ] == ' ' ) ? 1 : 0 ; if ( sum == 2 * k && mp . has ( k ) && i < s . length - 1 && i > 0 ) { res += mp . get ( k ) ; } if ( mp . has ( sum ) ) mp . set ( sum , mp . get ( sum ) + 1 ) else mp . set ( sum , 1 ) ; } return res ; } var str = " " ; document . write ( count ( str ) ) ;