Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 | Java 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
import java . util . * ; class GFG { public static int countSubstrings ( String s ) { int n = s . length ( ) ; int answer = ( n * ( n - 1 ) ) / 2 ; int cnt = 1 ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) cnt ++ ; else { v . add ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . add ( cnt ) ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { answer -= ( v . get ( i ) + v . get ( i + 1 ) - 1 ) ; } return answer ; } public static void main ( String [ ] args ) { String s = "00111" ; System . out . print ( countSubstrings ( s ) ) ; } }