Jump to content

regular expression of equal number of 'a' and 'b'


Recommended Posts

Hi,I had a job interview and they asked me about regular expression, I knew most most of the question. However they asked how can I define regular expression that will accept every string that contain equal number of the letter 'a' and 'b', regardless of the other chars.I mean string like:aaabbb (3 - 'a' , a - 'b')afffhgflhgfladflgkdflbdfdsfdsb (2 - 'a', 2 - 'b')avvvbbdda (2 - 'a', 2 - 'b') I told them I know how to create function that will just count the number of 'a' and 'b' and then equate them.but my question is, can I solve it with regular expression and how?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...