Given a binary string , count number of substrings that start and end with 1. | A Python3 program to count number of substrings starting and ending with 1 ; Count of 1 's in input string ; Traverse input string and count of 1 's in it ; Return count of possible pairs among m 1 's ; Driver program to test above function
def countSubStr ( st , n ) : NEW_LINE INDENT m = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( st [ i ] == '1' ) : NEW_LINE INDENT m = m + 1 NEW_LINE DEDENT DEDENT return m * ( m - 1 ) // 2 NEW_LINE DEDENT st = "00100101" ; NEW_LINE list ( st ) NEW_LINE n = len ( st ) NEW_LINE print ( countSubStr ( st , n ) , end = " " ) NEW_LINE