**Prerequisite:** Introduction to Deterministic Finite Automata

Construct a DFA that accepts a string **str** starting with input alphabet **‘a’** but does not contain **‘aab’** as a substring over input **{a, b}**.

**Examples:**

Input:str = “babba”

Output:Not Accepted

Explanation:

The given string doesn’t start with ‘a’.

Input:str = “abbaaaaa”

Output:Accepted

Explanation:

The given string start with ‘a’and doesn’t contains “aab” as a substring.

**Approach:**

The transition table helps to understand how the transition of each state takes place on the input alphabets. In transition table initial state is represented by **—>** and the final state is represented by *****. There are 3 final states, one initial and one dead state.

**State Transition table of the given DFA:**

STATE | INPUT (a) | INPUT (b) |

—> A | B* | Q (dead state) |

B* | C* | D* |

C* | C* | Q (dead state) |

D* | B* | D* |

Q (dead state) | Q (dead state) | Q (dead State) |

Below is the implementation of the above DFA:

`# Python code for the above DFA ` ` ` `# Function for state A transition ` `def` `stateA(n): ` ` ` ` ` `# If at index 0 ` ` ` `# 'a' if found then ` ` ` `# call stateB function ` ` ` `# with passing n[1:] to it ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `): ` ` ` `stateB(n[` `1` `:]) ` ` ` ` ` `# If at index 0 ` ` ` `# 'b' if found then ` ` ` `# call stateQ function ` ` ` `# with passing n to it ` ` ` `else` `: ` ` ` `stateQ(n) ` ` ` `# Function for state B transition ` `def` `stateB(n): ` ` ` ` ` `# Length of string ` ` ` `# become 0 then ` ` ` `# print Accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"Accepted"` `) ` ` ` `else` `: ` ` ` `# If at index 0 ` ` ` `# 'a' if found then ` ` ` `# call stateC function ` ` ` `# with passing n[1:] to it ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `): ` ` ` `stateC(n[` `1` `:]) ` ` ` ` ` `# If at index 0 ` ` ` `# 'b' if found then ` ` ` `# call stateD function ` ` ` `# with passing n[1:] to it ` ` ` `else` `: ` ` ` `stateD(n[` `1` `:]) ` ` ` `# Function for state C transition ` `def` `stateC(n): ` ` ` ` ` `# Length of string ` ` ` `# become 0 then ` ` ` `# print Accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"Accepted"` `) ` ` ` ` ` `else` `: ` ` ` `# If at index 0 ` ` ` `# 'a' if found then ` ` ` `# call stateC function ` ` ` `# with passing n[1:] to it ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `): ` ` ` `stateC(n[` `1` `:]) ` ` ` ` ` `# If at index 0 ` ` ` `# 'b' if found then ` ` ` `# call stateQ function ` ` ` `# with passing n to it ` ` ` `else` `: ` ` ` `stateQ(n) ` ` ` `# Function for state D transition ` `def` `stateD(n): ` ` ` ` ` `# Length of string ` ` ` `# become 0 then ` ` ` `# print Accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"Accepted"` `) ` ` ` ` ` `else` `: ` ` ` `# If at index 0 ` ` ` `# 'a' if found then ` ` ` `# call stateB function ` ` ` `# with passing n[1:] to it ` ` ` `if` `(n[` `0` `]` `=` `=` `'a'` `): ` ` ` `stateB(n[` `1` `:]) ` ` ` ` ` `# If at index 0 ` ` ` `# 'b' if found then ` ` ` `# call stateD function ` ` ` `# with passing n[1:] to it ` ` ` `else` `: ` ` ` `stateD(n[` `1` `:]) ` ` ` `# Function for state Q transition ` `def` `stateQ(n): ` ` ` `# In dead state ` ` ` `# it shows string ` ` ` `# not accepted ` ` ` `print` `(` `"Not Accepted"` `) ` ` ` `# Take string input ` `n ` `=` `"aaaba"` ` ` `# Call stateA to check the input ` `stateA(n) ` |

*chevron_right*

*filter_none*

**Output:**

Not Accepted

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:

- Next word that does not contain a palindrome and has characters from first k
- Print a closest string that does not contain adjacent duplicates
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Check if a String contains Anagrams of length K which does not contain the character X
- Length of the longest substring that do not contain any palindrome
- Number of substrings divisible by 8 but not by 3
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- DFA for Strings not ending with "THE"
- DFA for strings not containing consecutive two a's and starting with 'a'
- Count of sub-strings that do not contain all the characters from the set {'a', 'b', 'c'} at the same time
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Conversion from NFA to DFA
- Minimization of DFA
- DFA for accepting the language L = { a
^{n}b^{m}| n+m=even } - Program to build DFA that starts and end with 'a' from input (a, b)

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.