It wouldn't be possible if only the first constraint (have the same number of a's and b's
) was there. Because of the second constraint there is a solution to your problem.
It's easier to first think of the finite automaton that does your work (I leave this to you as an exercise) and then transform it to a regular expression.
The transformed regex would be:
[ (((a | (aab) (ab)*) b)* (((b | bba) (ba)*) a)* ]*
(perhaps it can be simplified, also left for you as an exercise).