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 Code
< ? php function checkEquality ( $ s ) { return ( $ s [ 0 ] == $ s [ strlen ( $ s ) - 1 ] ) ; } function countSubstringWithEqualEnds ( $ s ) { $ result = 0 ; $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ len = 1 ; $ len <= $ n - $ i ; $ len ++ ) if ( checkEquality ( substr ( $ s , $ i , $ len ) ) ) $ result ++ ; return $ result ; } $ s = " abcab " ; print ( countSubstringWithEqualEnds ( $ s ) ) ; ? >