Related Articles

# DFA of alternate 0’s and 1’s

• Difficulty Level : Medium
• Last Updated : 09 Sep, 2020

Regular Expression can be anything, from a terminal symbol, ∅, to union of two regular expressions (R1 + R2), their concatenation (R1R2) or its closure R1* as well.

Examples of Regular Expression :

1. Regular expression of set of all strings of 0’s and 1’s starting with two zeros :
`00(0+1)*`
2. Regular expression of set of all strings of 0’s and 1’s having even number of 0’s followed by odd numbers of 1’s :
`(00)*1(11)*`
3. Regular expression of set of all strings of 0’s and 1’s containing at least one 0 and at least two 1’s :
`00*11(0+1)* + 0111*(0+1)*`

Strings that will be acceptable by regular expression with alternate 0’s and 1’s –

1. ∈ (no input, 0 and 1)
2. 010101….. (string that starts with 0 followed by 1 and so on).
3. 101010….. (string that starts with 1 followed by 0 and so on).

Now, a regular expression for set of all strings consisting of alternate 0’s and 1’s would be (01)*, where it can accept ∈, 01, 0101, 010101…..etc but this restricts the string as it can always begin with 0 only.

Again, the expression (10)* will accept ∈, 10, 1010, 101010….etc but this too restricts string as it can always begin with 1 only.

So, we introduce 1(01)* and 0(10)* to meet gap in respective cases.

While 1(01)* breaks the restriction of string starting with 0, 0(10)* breaks same for string starting with 1.

So, the final expression is –

`(01)* + (10)* + 0(10)* + 1(01)*`

Figure – Finite Automata of Regular Expression for alternate 0’s and 1’s

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up