Since you have not described the actual problem you're trying to solve nor the constraints of the example, it's difficult to suggest solutions. For example, it would be useful to know:
How many comma-separated elements will be in the string?
Is B,A different than A,B?
You include B,B in your list, but not A,A. Is that intentional?
More specifically, can there be more than 9? If not, you could represent each position (i.e. each element) by a digit 1-9. Then your optimization loop becomes quite straightforward. You could also come up with a bitmapping scheme where each of the string elements is represented by a single bit.
I think on reflection I have got confused with what I am asking for here. And it's probably not do-able, because with 10 symbols in ee, the total combinations would be 10^10!
I will have to scale back the number of symbols in ee and use multiple Optimize loops, one for each symbol - each one on/off.
After some thought I know how to do it programmatically.
And you do not need repetitions like A,B and then B,A.
(The lists -symbol combinations- are created as explained here.)
If you need code then drop mail.