Prerequisite – Finite Automata Introduction

**Problem –** Draw deterministic finite automata (DFA) of the language containing the set of all strings over {a, b} in which 2nd symbol from RHS is ‘a’.

The strings in which 2nd last symbol is “a” are:

aa, ab, aab, aaa, aabbaa, bbbab etc

**For example:**

INPUT : baba OUTPUT: NOT ACCEPTED INPUT: aaab OUTPUT: ACCEPTED

Constructing the DFA of the given problem directly is very complicated. So, here we are going to design the non-deterministic finite automata (NFA) and then convert it to the deterministic finite automata (DFA).

The NFA of the language containing all the strings in which 2nd symbol from the RHS is “a” is:

Here, A is the initial state and C is the final state.

Now, we are going to construct the state transition table of the above NFA.

After that we will draw the state transition table of DFA using subset configuration on the state transition table of NFA. We will mention all the possible transition for a and b.

Now it’s become very easy to draw the DFA with the help of its transition table. In this DFA, we have four different states A, AB, ABC and AC, where ABC and AC are the final states and A is the initial state of the DFA.

This is our required DFA of the language containing the set of all strings over {a, b} in which 2nd symbol from RHS is ‘a’.

### Transition table:

STATES | INPUT (a) | INPUT (b) |

—> A (initial state) | AB | A |

AB | ABC* (final state) | AC* (final state) |

AC* (final state) | AB | A |

ABC* (final state) | ABC* (final state) | AC* (final state) |

### Python Implementation:

`def` `stateA(n): ` ` ` `#if length found 0 ` ` ` `#print not accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string not accepted"` `) ` ` ` `else` `: ` ` ` ` ` `#if at index 0 ` ` ` `#'a' found call ` ` ` `#function stateAB ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `): ` ` ` `stateAB(n[` `1` `:]) ` ` ` ` ` `#else if 'b' found ` ` ` `#call function A. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'b'` `): ` ` ` `stateA(n[` `1` `:]) ` ` ` `def` `stateAB(n): ` ` ` `#if length found 0 ` ` ` `#print not accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string not accepted"` `) ` ` ` `else` `: ` ` ` ` ` `#if at index 0 ` ` ` `#'a' found call ` ` ` `#function stateABC ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `): ` ` ` `stateABC(n[` `1` `:]) ` ` ` ` ` `#else if 'b' found ` ` ` `#call function AC. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'b'` `): ` ` ` `stateAC(n[` `1` `:]) ` ` ` `def` `stateABC(n): ` ` ` `#if length found 0 ` ` ` `#print accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string accepted"` `) ` ` ` `else` `: ` ` ` ` ` `#if at index 0 ` ` ` `#'a' found call ` ` ` `#function stateABC ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `): ` ` ` `stateABC(n[` `1` `:]) ` ` ` ` ` `#else if 'b' found ` ` ` `#call function AC. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'b'` `): ` ` ` `stateAC(n[` `1` `:]) ` ` ` `def` `stateAC(n): ` ` ` `#if length found 0 ` ` ` `#print accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string accepted"` `) ` ` ` `else` `: ` ` ` `#if at index 0 ` ` ` `#'a' found call ` ` ` `#function stateAB ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `): ` ` ` `stateAB(n[` `1` `:]) ` ` ` ` ` `#else if 'b' found ` ` ` `#call function A. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'b'` `): ` ` ` `stateA(n[` `1` `:]) ` ` ` ` ` `#take string input ` `n` `=` `input` `() ` ` ` `#call stateA ` `#to check the input ` `stateA(n) ` |

*chevron_right*

*filter_none*

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.

## Recommended Posts:

- DFA of a string in which 3rd symbol from RHS is ‘a’
- DFA in LEX code which accepts even number of zeros and even number of ones
- Program to construct a DFA which accept the language L = {a
^{n}b^{m}| n mod 2=0, m≥1} - Construct DFA which interpreted as binary number is divisible by 2, 3, 4
- Construct a DFA which accept the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}
- Construct a DFA which accept the language L = {a
^{n}b^{m}| n > =1, (m) mod 3 = 1} - DFA of a string with at least two 0’s and at least two 1’s
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- Design a DFA that accepts a string containing 3 a's and 3 b's
- Symbol Table in Compiler
- Various implementations of Symbol Table
- Conversion from NFA to DFA
- Minimization of DFA
- DFA for accepting the language L = { a
^{n}b^{m}| n+m=even } - Program to Implement NFA with epsilon move to DFA Conversion
- DFA for Strings not ending with "THE"
- Union process in DFA
- DFA machines accepting odd number of 0’s or/and even number of 1’s
- Concatenation process in DFA
- Complementation process in DFA

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.