Count substrings with same first and last characters | Returns true if first and last characters of s are same . ; Starting point of substring ; Length of substring ; Check if current substring has same starting and ending characters . ; Driver function
function checkEquality ( s ) { return ( s . charAt ( 0 ) == s . charAt ( s . length - 1 ) ) ; } function countSubstringWithEqualEnds ( s ) { var result = 0 ; var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) for ( var len = 1 ; len <= n - i ; len ++ ) if ( checkEquality ( s . substring ( i , i + len ) ) ) result ++ ; return result ; } var s = " " ; document . write ( countSubstringWithEqualEnds ( s ) ) ;