total_beauty to store the sum of beauties of all substrings.i representing the start of the substring and the inner loop index j representing the end of the substring.i, initialize a frequency array freq of size 26 to 0 (for 26 lowercase English letters).j from i to the end of the string, increment the frequency of the character s[j] in the freq array.freq array.total_beauty.i and repeat steps 3-6.total_beauty as the final result.