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
def countSubsequence ( S , N , K ) : NEW_LINE INDENT C = 0 NEW_LINE C1 = 0 NEW_LINE C0 = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT C1 += 1 NEW_LINE DEDENT elif ( S [ i ] == '0' ) : NEW_LINE INDENT C0 += 1 NEW_LINE DEDENT DEDENT B1 = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT B1 += 1 NEW_LINE DEDENT elif ( S [ i ] == '0' ) : NEW_LINE INDENT C = C + ( C1 - B1 ) NEW_LINE DEDENT DEDENT ans = C * K NEW_LINE ans += ( C1 * C0 * ( ( ( K ) * ( K - 1 ) ) // 2 ) ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "230013110087" NEW_LINE K = 2 NEW_LINE N = len ( S ) NEW_LINE print ( countSubsequence ( S , N , K ) ) NEW_LINE DEDENT