Regular expression for strings with even number of a's and odd no of b's
Asked Answered
P

13

20

Im having a problem in solving the problem:- Its an assignment, i solved it, but it seems to be too long and vague, Can anyboby help me please......

Regular expression for the strings with even number of a's and odd number of b's where the character set={a,b}.

Pantaloon answered 13/9, 2010 at 7:50 Comment(4)
You should post your solution so that people can suggest specific improvements.Corporal
What is the Regex supposed to achieve? How many sequences of 'a's and 'b's are there? Can you show how you solved it and that may help in determining a regex that could do the same.Coralyn
You dont want to use RegEx here - the assignment should be solved with a simple "dont!" and a few lines of code.Frants
get an idea from my answer here: How to write regular expression for a DFA using Arden theoremNorword
I
20

One way to do this is to pass it through two regular expressions making sure they both match (assuming you want to use regular expressions at all, see below for an alternative):

^b*(ab*ab*)*$
^a*ba*(ba*ba*)*$

Anything else (and, in fact, even that) is most likely just an attempt to be clever, one that's generally a massive failure.

The first regular expression ensures there are an even number of a with b anywhere in the mix (before, after and in between).

The second is similar but ensures that there's an odd number of b by virtue of the starting a*ba*.


A far better way to do it is to ignore regular expressions altogether and simply run through the string as follows:

def isValid(s):
    set evenA to true
    set oddB to false
    for c as each character in s:
        if c is 'a':
            set evenA to not evenA
        else if c is 'b':
            set oddB to  not oddB
        else:
            return false
    return evenA and oddB

Though regular expressions are a wonderful tool, they're not suited for everything and they become far less useful as their readability and maintainability degrades.


For what it's worth, a single-regex answer is:

(aa|bb|(ab|ba)(aa|bb)*(ba|ab))*(b|(ab|ba)(bb|aa)*a)

but, if I caught anyone on my team actually using a monstrosity like that, they'd be sent back to do it again.

This comes from a paper by one Greg Bacon. See here for the actual inner workings.

Ignatius answered 13/9, 2010 at 7:59 Comment(0)
D
7
Even-Even = (aa+bb+(ab+ba)(aa+bb)*(ab+ba))*

(Even-Even has even number of Aas and b's both)

Even a's and odd b's = Even-Even b Even-Even

This hsould work

Dollop answered 9/9, 2018 at 17:12 Comment(0)
L
3

This regular expression takes all strings with even number of a's and even number of b's

r1=((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*

Now to get regular expression for even a's and odd b's

r2=(b+a(aa+bb)*(ab+ba))((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*
Longcloth answered 10/10, 2017 at 13:37 Comment(0)
S
1
  1. (bb)*a(aa)*ab(bb)*
  2. ab(bb)* a(aa)*
  3. b(aa)*(bb)* .
    .
    .
    .
    .
    .

there can be many such regular expressions. Do you have any other condition like "starting with a" or something of the kind (other than odd 'b' and even 'a') ?

Sachs answered 13/9, 2010 at 8:0 Comment(1)
i was locking for even number of b's and odd number of a's so i modified you Regex (aa)*b(bb)*ba(aa)* and works for me thankx manFlowering
M
1

For even number of a's and b's , we have regex:

E = { (ab + ba) (aa+bb)* (ab+ba) }*

For even number of a's and odd number of b's , all we need to do is to add an extra b in the above expression E.

The required regex will be:

E = { ((ab + ba) (aa+bb)* (ab+ba))* b ((ab + ba) (aa+bb)* (ab+ba))* }
Monolingual answered 23/11, 2018 at 9:45 Comment(0)
L
0

I would do as follows:

  • regex even matches the symbol a, then a sequence of b's, then the symbol a again, then another sequence of b's, such that there is an even number of b's:

even -> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)

  • regex odd does the same with an odd total number of b's:

odd -> (a b (bb)* a (bb)* | a (bb)* a b (bb)*)

A string of even number of a's and odd number of b's either:

  • starts with an odd number of b's, and is followed by an even number of odd patterns amongst even patterns;
  • or starts with an even number of b's, and is followed by an odd number of odd patterns amongst even patterns.

Note that even has no incidence on the evenness/oddness of the a/b's in the string.

regex -> (

b (bb)* even* (odd even* odd)* even*

|

(bb)* even* odd even* (odd even* odd)* even*

)

Of course one can replace every occurence of even and odd in the final regex to get a single regex.

It is easy to see that a string satisfying this regex will indeed have an even number of a's (as symbol a occurs only in even and odd subregexes, and these each use exactly two a's) and an odd number of b's (first case : 1 b + even number of b's + even number of odd; second case : even number of b's + odd number of odd).

A string with an even number of a's and an odd number of b's will satisfy this regex as it starts with zero or more b's, then is followed by [one a, zero or more b's, one more a and zero or more b's], zero or more times.

Lordsandladies answered 21/10, 2016 at 10:45 Comment(0)
A
0

A high-level advice: construct a deterministic finite automaton for the language---very easy, encode parity of the number of as and bs in the states, with q0 encoding even nr. of as and even nr. of bs, and transition accordingly---, and then convert the DFA into a regular expression (either by using well-known algorithms for this or "from scratch").

The idea here is to exploit the well-understood equivalence between the DFA (an algorithmic description of regular languages) and the regular expressions (an algebraic description of regular languages).

Alcatraz answered 13/12, 2016 at 13:39 Comment(0)
C
0

The regular expression are given below :

    (aa|bb)*((ab|ba)(aa|bb)*(ab|ba)(aa|bb)*b)*
Cynosure answered 1/12, 2017 at 18:5 Comment(0)
W
0

The structured way to do it is to make one transition diagram and build the regular expression from it. The regex in this case will be

(a((b(aa)*b)*a+b(aa)*ab)+b((a(bb)*a)*b+a(bb)*ba))b(a(bb)*a)*

It looks complicated but it covers all possible cases that may arise.

Weaks answered 2/2, 2019 at 5:25 Comment(0)
E
-1

the answer is (aa+ab+ba+bb)* b (aa+ab+ba+bb)*

Engross answered 30/11, 2014 at 12:7 Comment(1)
it can accept baaab that have odd number of a's and even number of b'sSenter
P
-1

(bb)* b (aa)* + (aa)* b (bb)*

This is the answer which handles all kind of strings with odd b's and even a's.

Pathogenesis answered 25/2, 2016 at 9:41 Comment(0)
O
-2

If it is even number of a's followed by odd number of b's (aa)*b(bb)* should work

if it is in any order (aa)*b(bb)* + b(bb)(aa) should work

Outhe answered 18/12, 2016 at 3:51 Comment(0)
H
-2

All strings that have even no of a's and odd no of b's (((aa+bb) * b(aa+bb) * ) + (A +((a+b)b(a+b)) *)) *

here A is for null string. A can be neglected.

if there is any error plz point it out.

Hon answered 9/4, 2017 at 5:20 Comment(1)
This question is pretty old and already have couple of answers. Put your efforts to answer new questions. Also have a look at How do I write a good answerGearbox

© 2022 - 2024 — McMap. All rights reserved.