Count of sub | Function to find the count of substrings with equal no . of consecutive 0 ' s ▁ and ▁ 1' s ; To store the total count of substrings ; Traversing the string ; Count of consecutive 0 ' s ▁ & ▁ 1' s ; Counting subarrays of type "01" ; Count the consecutive 0 's ; If consecutive 0 ' s ▁ ▁ ends ▁ then ▁ check ▁ for ▁ ▁ consecutive ▁ 1' s ; Counting consecutive 1 's ; Counting subarrays of type "10" ; Count consecutive 1 's ; If consecutive 1 ' s ▁ ▁ ends ▁ then ▁ check ▁ for ▁ ▁ consecutive ▁ 0' s ; Count consecutive 0 's ; Update the total count of substrings with minimum of ( cnt0 , cnt1 ) ; Return answer ; Driver code ; Function to print the count of substrings
def countSubstring ( S , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT cnt0 = 0 ; cnt1 = 0 ; NEW_LINE if ( S [ i ] == '0' ) : NEW_LINE INDENT while ( i < n and S [ i ] == '0' ) : NEW_LINE INDENT cnt0 += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT j = i ; NEW_LINE while ( j < n and S [ j ] == '1' ) : NEW_LINE INDENT cnt1 += 1 ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT while ( i < n and S [ i ] == '1' ) : NEW_LINE INDENT cnt1 += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT j = i ; NEW_LINE while ( j < n and S [ j ] == '0' ) : NEW_LINE INDENT cnt0 += 1 ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT ans += min ( cnt0 , cnt1 ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "0001110010" ; NEW_LINE n = len ( S ) ; NEW_LINE print ( countSubstring ( S , n ) ) ; NEW_LINE DEDENT