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