Sum of all distances between occurrences of same characters in a given string | C # program for the above approach ; Function to calculate the sum of distances between occurrences of same characters in a String ; If similar characters are found ; Add the difference of their positions ; Return the answer ; Driver Code
using System ; class GFG { static int findSum ( String s ) { int sum = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { for ( int j = i + 1 ; j < s . Length ; j ++ ) { if ( s [ i ] == s [ j ] ) { sum += ( j - i ) ; } } } return sum ; } public static void Main ( String [ ] args ) { String s = " ttt " ; Console . Write ( findSum ( s ) + " STRNEWLINE " ) ; } }