Search code examples
regular-language

Regular expression to form words with exactly 4 a's or 5 b's


In formal language theory (with Kleene Star) if I wanted to create a regualr expression that defines the language containing all words over the Set {a ,b}, with exactly 4 a's or 5 b's.

(note the book I have uses + for OR, and * for 0 or many)

So far I have:

[a(b*)aaa + aa(b*)aa + aaa(b*)a + aaaa(b*) + abababab + babababa + (b*)aaaa]

+

[b(a*)bbbb + bb(a*)bbb + bbb(a*)bb + bbbb(a*)b + bbbbb(a*) + (a*)bbbbb + bababababa + ababababab]

Is there a shorten way to do this? It does not seem that theory allows the use of min and max for letters.


Solution

  • Your expression doesn't suit all possibilities (e.g. babababababbbb). The searched expression should be much easier:

    [ b*ab*ab*ab*ab* + a*ba*ba*ba*ba*ba* ]