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
function findSum ( s ) { let sum = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { for ( let j = i + 1 ; j < s . length ; j ++ ) { if ( s [ i ] == s [ j ] ) { sum += ( j - i ) ; } } } return sum ; } let s = " " ; document . write ( findSum ( s ) + " " ) ;