Count substrings with same first and last characters | Space efficient javascript program to count all substrings with same first and last characters . ; Iterating through all substrings in way so that we can find first and last character easily ; Driver code
function countSubstringWithEqualEnds ( s ) { var result = 0 ; var n = s . length ; for ( i = 0 ; i < n ; i ++ ) for ( j = i ; j < n ; j ++ ) if ( s . charAt ( i ) == s . charAt ( j ) ) result ++ ; return result ; } var s = " " ; document . write ( countSubstringWithEqualEnds ( s ) ) ;