Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 | 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 ; Given string s ; Function call
function countSubstrings ( s ) { var n = s . length ; var answer = ( n * ( n - 1 ) ) / 2 ; var cnt = 1 ; var v = [ ] ; for ( var i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) cnt ++ ; else { v . push ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . push ( cnt ) ; for ( var i = 0 ; i < v . length - 1 ; i ++ ) { answer -= ( v [ i ] + v [ i + 1 ] - 1 ) ; } return answer ; } var s = " " ; document . write ( countSubstrings ( s ) ) ;