Iterate till the length of the given string
for ( int i = 0 ; i < S . size ( ) ; i ++ ) { int now = S [ i ] - ' a ' ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j ] [ now ] += arr [ j ] ; } arr [ now ] ++ ; } long long int ans = 0 ;