I need a regular expression which will describe the strings of the alphabet {a, b, c} of the language {a^n b^m c^k, where n + m + k is even for variable integers n, m, k ≥ 0}. For example, the strings ε, aabc, abbccccc belong to the language, while the strings abccaa, a, aaabbcc
do not belong.
Should I build first a Finite Automaton or is it possible without it?
Any help will be appreciated!
>Solution :
Interpreting this as a programming question (which is what Stack Overflow is about), you could use this regular expression:
Explanation:
^and$match the start/end of the input.(..)*matches a series of pairs of any characters, so it always matches an even number of characters.(?=(..)*$)is a look-ahead expression that verifies that the total length is even.a*b*c*matches any number ofa, followed by any number ofb, followed by any number ofc.
Without look-ahead, you’d need to foresee the cases where all groups are even, or just one group is even (and the two others odd):
^(aa)*((bb)*(bc)?(cc)*|a(bb)*[bc](cc)*)$