The task is similar to this one. Finding the largest balanced subarray (with equal number of each symbol). The difference is that the array consists of 4 different characters, not 0s and 1s. So the question would be how to change the approach with prefix sum in this case? Time complexity has to be no more than O(n^2), the string length is at most 10^5.
I was thinking about giving two characters values 1 and -1 and the other two 1000000 and -1000000, but it wouldn't work, simply because you can have a zero sum with half the symbols. Is there a simple way to do this?