10

I have a problem in solving the following exercise and I'd appreciate any help.

Let Σ = {a,b}. I need to give a regular expression for all strings containing an odd number of a.

Thank you for your time

kkyr
  • 3,785
  • 3
  • 29
  • 59

2 Answers2

21
b*(ab*ab*)*ab*

the main part of it is (ab*ab*)*, which enumerate all possibilities of even number of as. then at last, an extra a has to exist to make it odd.

notice that this regular expression is equivalent to:

b*a(b*ab*a)*b*

these two constructs are in the form defined by pumping lemma:

http://en.wikipedia.org/wiki/Pumping_lemma


UPDATE:

@MahanteshMAmbi presented his concern of the regular expression matching the case aaabaaa. In fact, it doesn't. If we run grep, we shall see clearly what is matched.

$ echo aaabaaa | grep -P -o 'b*(ab*ab*)*ab*'
aaabaa
a

-o option of grep will print each matching instance every line. In this case, as we can see, the regular expression is being matched twice. One matches 5 as, one matches 1 a. The seeming error in my comment below is caused by an improper test case, rather than the error in the regular expression.

If we want to make it rigorous to use in real life, it's probably better to use anchors in the expression to force a complete string match:

^b*(ab*ab*)*ab*$

therefore:

$ echo aaabaaa | grep -P -q '^b*(ab*ab*)*ab*$'
$ echo $?
1
Jason Hu
  • 6,239
  • 1
  • 20
  • 41
  • @MahanteshMAmbi you missed the stars after `b`s inside of the parenthesis my friend. – Jason Hu Aug 31 '17 at 15:20
  • @MahanteshMAmbi `$ echo aaabaaa | grep -P 'b*(ab*ab*)*ab*'` --> `aaabaaa` – Jason Hu Aug 31 '17 at 15:23
  • @MahanteshMAmbi to help you understand further why this is complete as regular expression. in pumping lemma, regular expression is defined to be generated in the form of `pr*q`, there is only one star of an expression `r`. therefore I for sure know it covers all the cases. for more details, you can read the pumping lemma page i pasted up there. – Jason Hu Aug 31 '17 at 15:32
  • @HuStmpHrrr : I think the question is about word having odd numbers of a's. So aaabaaa is not a valid word. Your regular expression must not allow even number of a's. – Mahantesh M Ambi Sep 01 '17 at 01:54
  • @HuStmpHrrr : Thanks for bringing aaabaaa example, the question was asked to give a regular expression which doesn't contain even no of a's. Anyway aaabaa will be accepted by your regular expression but aaabaaa shouldn't be. But it does. So it's wrong is what I am claiming – Mahantesh M Ambi Sep 01 '17 at 02:01
  • 1
    @MahanteshMAmbi I updated the answer. that should answer you question. the original answer was correct. – Jason Hu Sep 01 '17 at 03:20
  • @HuStmpHrrr: Yeah you were right, aaabaaa would not be accepted by your regular expression. – Mahantesh M Ambi Sep 21 '17 at 16:49
  • @JasonHu I guess you have a follow-up question https://stackoverflow.com/q/58590857/823738 – Zaffy Oct 28 '19 at 12:34
1
^[^a]*a(?=[^a]*(?:a[^a]*a)*[^a]*$).*$

This will find only odd number of a's for any generic string.See demo.

https://regex101.com/r/eS7gD7/22

vks
  • 67,027
  • 10
  • 91
  • 124
  • Thank you but the answer I was looking for is exactly like the one HuStmpHrr provided – kkyr Mar 06 '15 at 16:09