Count substrings with same first and last characters | C # program to count all 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
using System ; class GFG { static bool checkEquality ( string s ) { return ( s [ 0 ] == s [ s . Length - 1 ] ) ; } static int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) if ( checkEquality ( s . Substring ( i , len ) ) ) result ++ ; return result ; } public static void Main ( ) { string s = " abcab " ; Console . WriteLine ( countSubstringWithEqualEnds ( s ) ) ; } }