The problem can be greatly simplified with a little math.
Let a
be number of 0, b
- number of 1, n
- length of a word. We want abs(a - b)
to be even.
Do the math:
a + b = n
b = n - a
a - b = a - n + a = 2*a - n
2*a
is always even, so abs(a - b)
is even iff n
is even.
So the task is really just to check if the length of the word is even.
Solution:
s --> [].
s --> digit, digit, s.
digit --> [0].
digit --> [1].