Count ways to partition a Binary String such that each substring contains exactly two 0 s | Function to find count of ways to partition the string such that each partition contains exactly two 0 s . ; Stores indices of 0 s in the given string . ; Store the count of ways to partition the string such that each partition contains exactly two 0 s . ; Iterate over each characters of the given string ; If current character is '0' ; Insert index ; Stores total count of 0 s in str ; Traverse the array , IdxOf0s [ ] ; Update cntWays ; Driver Code
function totalWays ( n , str ) { var IdxOf0s = [ ] ; var cntWays = 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { IdxOf0s . push ( i ) ; } } var M = IdxOf0s . length ; if ( M == 0 M % 2 ) { return 0 ; } for ( var i = 2 ; i < M ; i += 2 ) { cntWays = cntWays * ( IdxOf0s [ i ] - IdxOf0s [ i - 1 ] ) ; } return cntWays ; } var str = " " ; var n = str . length ; document . write ( totalWays ( n , str ) ) ;