Official

A - XXYYX Editorial by evima


If we fix the first and last characters of \(S\), the difference between the numbers of XY and YX will be uniquely determined, independent of the middle characters. For instance, if \(S\) begins with X and ends with Y, then XY must occur one more time than YX, and we have \(B - C = 1\). By considering the other cases similarly, we can see that \(|B - C| \le 1\) must hold for the answer to be Yes.

When \(|B - C| = 1\), a desired string \(S\) always exists. For now, let us ignore the conditions on \(A\) and \(D\) and let:

  • \(S' = {}\)XYXY…XY (the concatenation of \(B\) XYs) if \(B - C = 1\), and
  • \(S' = {}\)YXYX…YX (the concatenation of \(C\) YXs) if \(B - C = -1\).

Then, add \((A - 1)\) Xs and \((D - 1)\) Ys at the beginning and end of \(S'\) accordingly to obtain a desired string \(S\).

When \(|B - C| = 0\), the answer is again Yes if \(B > 0\). You can, for instance, let \(S' = {}\)XYXY…XYX (the concatenation of \(B\) XYs followed by one X) and then add \((A - 1)\) Xs and \((D - 1)\) Ys in \(S'\) accordingly to obtain a desired string \(S\).

Finally, when \(B = C = 0\), the only possible forms of the string are XXX…X and YYY…Y, so the answer is Yes if and only if at least one of \(A\) and \(D\) is \(0\).

Sample implementation

posted:
last update: