Given a binary string , count number of substrings that start and end with 1. | A O ( n ) C # program to count number of substrings starting and ending with 1 ; Traverse input string and count of 1 's in it ; Return count of possible pairs among m 1 's ; Driver Code
using System ; class GFG { int countSubStr ( char [ ] str , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) m ++ ; } return m * ( m - 1 ) / 2 ; } public static void Main ( String [ ] args ) { GFG count = new GFG ( ) ; String strings = "00100101" ; char [ ] str = strings . ToCharArray ( ) ; int n = str . Length ; Console . Write ( count . countSubStr ( str , n ) ) ; } }