Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 | C # implementation to find the substrings in binary string such that every character belongs to a palindrome ; Function to to find the substrings in binary string such that every character belongs to a palindrome ; Total substrings ; Loop to store the count of continious characters in the given string ; Subtract non special strings from answer ; Driver code ; Given string s ; Function call
using System ; using System . Collections . Generic ; class GFG { public static int countSubstrings ( String s ) { int n = s . Length ; int answer = ( n * ( n - 1 ) ) / 2 ; int cnt = 1 ; List < int > v = new List < int > ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) cnt ++ ; else { v . Add ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . Add ( cnt ) ; for ( int i = 0 ; i < v . Count - 1 ; i ++ ) { answer -= ( v [ i ] + v [ i + 1 ] - 1 ) ; } return answer ; } public static void Main ( String [ ] args ) { String s = "00111" ; Console . Write ( countSubstrings ( s ) ) ; } }