Help with explanation

https://codeforces.com/contest/1494/problem/A

     d[x] = 1; d[y] = -1;
  if (count(s.begin(), s.end(), 'A' + x) == s.length() / 2)
    d[3 ^ x ^ y] = -1;
  else
    d[3 ^ x ^ y] = 1;
  int bal = 0;
  for (char c : s) {
    bal += d[c - 'A'];
    if (bal < 0) return false;
  }
  return bal == 0;
}`

please explain this part of the code (it’s from the editorial)

hello @raghav007

3^x^y this is nothing but finding the 3rd number.

note ->
A is mapped to 0
B is mapped to 1
C is mapped to 2
take its xor , it will be 3.
now if u know any two of them u can get third by simply taking their xor with 3 (i.e 3^x^y).
rest logic is just same as checking balanced paranthiesis.

so what this line is just doing is checking if sum of opening and closing brackets = 1/2 the length of the string but how do we know that A + x represents opening or closing. In the editorial it is written that we check if the count of first letter and remaining letter is equal to half of the len of string but here we are using only one letter represented by A + x

A+x will be opening bracket na, becuase it is the very first char of given string and we know first char must be (.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.