Count substrings with same first and last characters | Space efficient C # 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 function
using System ; public class GFG { static int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( s [ i ] == s [ j ] ) result ++ ; return result ; } public static void Main ( ) { string s = " abcab " ; Console . Write ( countSubstringWithEqualEnds ( s ) ) ; } }