Count subsequences 01 in string generated by concatenation of given numeric string K times | Function to calculate the number of subsequences of "01" ; Store count of 0 ' s ▁ and ▁ 1' s ; Count of subsequences without concatenation ; Case 1 ; Case 2 ; Return the total count ; Driver Code
function countSubsequence ( S , N , K ) { let C = 0 , C1 = 0 , C0 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == " " ) C1 ++ ; else if ( S [ i ] == " " ) C0 ++ ; } let B1 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == " " ) B1 ++ ; else if ( S [ i ] == " " ) C = C + ( C1 - B1 ) ; } let ans = C * K ; ans += C1 * C0 * ( ( K * ( K - 1 ) ) / 2 ) ; return ans ; } let S = " " ; let K = 2 ; let N = S . length ; document . write ( countSubsequence ( S , N , K ) ) ;