Regular Expression for Simple Language

134 Views Asked by At

I'm having trouble writing a regular expression given the following $\{a, b, c\}$ which produces the set of strings of length 3. I don't really understand how to restrict the length of the string. Obviously you could have 3 $a$'s, or 3 $b$'s or 3 $c$'s, but you could have $aab, aac, aba, \dots $

Is it something like $a^* \cup b^* \cup c^*$?

5

There are 5 best solutions below

0
On BEST ANSWER

The regular expression you have given gives the language $$\{\varepsilon, a, aa, aaa, \dots\} \cup \{\varepsilon, b, bb, bbb, \dots\} \cup \{\varepsilon, c, cc, ccc, \dots\}$$ $$= \{\varepsilon, a, aa, aaa, \dots, b, bb, bbb, \dots, c, cc, ccc, \dots\},$$ which is clearly not what you are after.

Using only union ($\cup$), concatenation ($\circ$), and the Kleene star ($^*$), you could do something like $$(a \cup b \cup c) \circ (a \cup b \cup c) \circ (a \cup b \cup c).$$

2
On

Something like this?

/[abc]{3}/g
0
On

If you're looking for a purely algebraic answer, you could do this:

(a|b|c)(a|b|c)(a|b|c)
0
On

For completeness, I will mention:

$$ \mathbf{aaa}\cup \mathbf{aab}\cup \mathbf{aac}\cup \mathbf{aba}\cup \mathbf{abb}\cup \mathbf{abc}\cup \mathbf{aca}\cup \mathbf{acb}\cup \mathbf{acc}\cup\\ \mathbf{baa}\cup \mathbf{bab}\cup \mathbf{bac}\cup \mathbf{bba}\cup \mathbf{bbb}\cup \mathbf{bbc}\cup \mathbf{bca}\cup \mathbf{bcb}\cup \mathbf{bcc}\cup\\ \mathbf{caa}\cup \mathbf{cab}\cup \mathbf{cac}\cup \mathbf{cba}\cup \mathbf{cbb}\cup \mathbf{cbc}\cup \mathbf{cca}\cup \mathbf{ccb}\cup \mathbf{ccc} $$

It's useful to remember that finite languages are trivially regular.

Also, regular expressions do not have to be clever.

0
On

I think it can be simply written as $((a+b+c)^3)^*$ since $(a+b+c)^3$ will produce all strings of length $3$.