Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Think about how to solve the problem if the string had only two distinct characters.
If we replace all occurrences of the first character by +1 and those of the second character by -1, can we efficiently calculate the largest possible variance of a string with only two distinct characters?
Now, try finding the optimal answer by taking all possible pairs of characters into consideration.