Reputation: 217
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}.
Upvotes: 20
Views: 158691
Reputation: 1
(bb)* b (aa)* + (aa)* b (bb)*
This is the answer which handles all kinds of strings with odd b's and even a's.
but it satisfies only string that has consecutive a or b not for single a and b
Upvotes: -1
Reputation: 705
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.
Upvotes: 0
Reputation: 11
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))* }
Upvotes: 1
Reputation: 71
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
Upvotes: 7
Reputation: 3725
The regular expression are given below :
(aa|bb)*((ab|ba)(aa|bb)*(ab|ba)(aa|bb)*b)*
Upvotes: 0
Reputation: 31
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))*
Upvotes: 3
Reputation: 1
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.
Upvotes: -2
Reputation: 1
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
Upvotes: -2
Reputation: 4845
A high-level advice: construct a deterministic finite automaton for the language---very easy, encode parity of the number of a
s and b
s in the states, with q0
encoding even nr. of a
s and even nr. of b
s, 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).
Upvotes: 0
Reputation: 3210
I would do as follows:
even -> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)
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:
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.
Upvotes: 0
Reputation: 881573
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.
Upvotes: 20
Reputation: 1182
(bb)*a(aa)*ab(bb)*
ab(bb)* a(aa)*
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') ?
Upvotes: 1