Sum of all distances between occurrences of same characters in a given string | 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
def findSum ( s ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == s [ j ] ) : NEW_LINE INDENT sum += ( j - i ) NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT s = " ttt " NEW_LINE print ( findSum ( s ) ) NEW_LINE