Find distinct characters in distinct substrings of a string | C # implementation of the approach ; Function to return the count of distinct characters in all the distinct sub - strings of the given string ; To store all the sub - strings ; To store the current sub - string ; To store the characters of the current sub - string ; If current sub - string hasn 't  been stored before ; Insert it into the set ; Update the count of distinct characters ; Driver code
using System ; using System . Collections . Generic ; class geeks { public static int countTotalDistinct ( String str ) { int cnt = 0 ; HashSet < String > items = new HashSet < String > ( ) ; for ( int i = 0 ; i < str . Length ; ++ i ) { String temp = " " ; HashSet < char > ans = new HashSet < char > ( ) ; for ( int j = i ; j < str . Length ; ++ j ) { temp = temp + str [ j ] ; ans . Add ( str [ j ] ) ; if ( ! items . Contains ( temp ) ) { items . Add ( temp ) ; cnt += ans . Count ; } } } return cnt ; } public static void Main ( String [ ] args ) { String str = " ABCA " ; Console . WriteLine ( countTotalDistinct ( str ) ) ; } }