DFA that begins with 'a' but does not contain substring 'aab' - GeeksforGeeks

09 Aug, 2021
Prerequisite: Introduction to Deterministic Finite Automata Construct a DFA that accepts 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 the 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:  
Below is the DFA diagram: 
Below is the implementation of the above DFA: 
C++
Java
Python3
C#
Javascript
// C++ code for the above DFA#include <bits/stdc++.h>using namespace std;void stateQ(string);void stateA(string);void stateB(string);void stateC(string);void stateD(string); // Function for state Q// transitionvoid stateQ(string n){  // In dead state  // it shows string  // not accepted  cout << ("Not Accepted");} // Function for state A// transitionvoid stateA(string n){  // If at index 0  // 'a' if found then  // call stateB function  // with passing n[1:] to it  if (n[0] == 'a')    stateB(n.substr(           1, n.length() + 1));   // If at index 0  // 'b' if found then  // call stateQ function  // with passing n to it  else    stateQ(n);} // Function for state B transitionvoid stateB(string n){  // Length of string  // become 0 then  // print Accepted  if (n.length() == 0)    cout << ("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.substr(             1, n.length() - 1));     // If at index 0    // 'b' if found then    // call stateD function    // with passing n[1:] to it    else      stateD(n.substr(             1, n.length() - 1));  }} // Function for state C// transitionvoid stateC(string n){  // Length of string  // become 0 then  // print Accepted  if (n.length() == 0)    cout << ("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.substr(             1, n.length() + 1));     // If at index 0    // 'b' if found then    // call stateQ function    // with passing n to it    else      stateQ(n);  }} // Function for state D// transitionvoid stateD(string n){  // Length of string  // become 0 then  // print Accepted  if (n.length() == 0)    cout << ("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.substr(             1, n.length() + 1));     // If at index 0    // 'b' if found then    // call stateD function    // with passing n[1:] to it    else      stateD(n.substr(             1, n.length() + 1));  }} // Driver codeint main(){  // Take string input  string n = "aaaba";   // Call stateA to check  // the input  stateA(n);} // This code is contributed by Chitranayal
// Java code for the// above DFAimport java.util.*; class GFG{      // Function for state// A transition   static void stateA(String n){     // If at index 0  // 'a' if found then  // call stateB function  // with passing n[1:] to it  if (n.charAt(0) == 'a')  {    stateB(n.substring(1));  }     // If at index 0  // 'b' if found then  // call stateQ function  // with passing n to it     else  {    stateQ(n);    }}   // Function for transition// state B        static void stateB(String n){     // length() of String  // become 0 then  // print Accepted  if (n.length() == 0)  {    System.out.print("Accepted");  }  else  {         // If at index 0    // 'a' if found then    // call stateC function    // with passing n[1:] to it    if (n.charAt(0) == 'a')      stateC(n.substring(1));      // If at index 0    // 'b' if found then    // call stateD function    // with passing n[1:] to it    else      stateD(n.substring(1));  }    }  // Function for transition// state Cstatic void stateC(String n){     // length() of String  // become 0 then  // print Accepted  if (n.length() == 0)    System.out.print("Accepted");  else  {         // If at index 0    // 'a' if found then    // call stateC function    // with passing n[1:] to it    if (n.charAt(0) == 'a')      stateC(n.substring(1));      // If at index 0    // 'b' if found then    // call stateQ function    // with passing n to it    else      stateQ(n);  }}  // Function for transition// state Dstatic void stateD(String n){     // length() of String  // become 0 then  // print Accepted  if (n.length() == 0)    System.out.print("Accepted");  else  {         // If at index 0    // 'a' if found then    // call stateC function    // with passing n[1:] to it    if (n.charAt(0) == 'a')    {      stateB(n.substring(1));    }      // If at index 0    // 'b' if found then    // call stateQ function    // with passing n to it    else    {      stateD(n.substring(1));    }  }}  // Function for state Q// transitionstatic void stateQ(String n){     // In dead state  // it shows String  // not accepted  System.out.print("Not Accepted");}      // Driver codepublic static void main(String []args){     // Take String input  String n ="aaaba";     // Call stateA to check the input  stateA(n);}} // This code is contributed by pratham76
# Python3 code for the above DFA # Function for state A transitiondef 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 transitiondef 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 transitiondef 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 transitiondef 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 transitiondef stateQ(n):    # In dead state    # it shows string    # not accepted    print("Not Accepted")     # Take string inputn ="aaaba" # Call stateA to check the inputstateA(n)
// C# code for the// above DFAusing System;using System.Collections;using System.Collections.Generic;class GFG{     // Function for state// A transition   static void stateA(string n){  // If at index 0  // 'a' if found then  // call stateB function  // with passing n[1:] to it  if (n[0] == 'a')  {    stateB(n.Substring(1));  }    // If at index 0  // 'b' if found then  // call stateQ function  // with passing n to it     else  {    stateQ(n);    }}  // Function for transition// state B        static void stateB(string n){  // Length of string  // become 0 then  // print Accepted  if (n.Length == 0)  {    Console.Write("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.Substring(1));     // If at index 0    // 'b' if found then    // call stateD function    // with passing n[1:] to it    else      stateD(n.Substring(1));  }    } // Function for transition// state Cstatic void stateC(string n){  // Length of string  // become 0 then  // print Accepted  if (n.Length == 0)    Console.Write("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.Substring(1));     // If at index 0    // 'b' if found then    // call stateQ function    // with passing n to it    else      stateQ(n);  }} // Function for transition// state Dstatic void stateD(string n){  // Length of string  // become 0 then  // print Accepted  if (n.Length == 0)    Console.Write("Accepted");  else  {    // If at index 0    // 'a' if found then    // call stateC function    // with passing n[1:] to it    if (n[0] == 'a')    {      stateB(n.Substring(1));    }     // If at index 0    // 'b' if found then    // call stateQ function    // with passing n to it    else    {      stateD(n.Substring(1));    }   }} // Function for state Q// transitionstatic void stateQ(string n){  // In dead state  // it shows string  // not accepted  Console.Write("Not Accepted");}     // Driver codepublic static void Main(string []args){  // Take string input  string n ="aaaba";   // Call stateA to check the input  stateA(n);}} // This code is contributed by rutvik_56
<script> // JavaScript program to implement// the above approach  // Function for state// A transition  function 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.substr(1));  }      // If at index 0  // 'b' if found then  // call stateQ function  // with passing n to it    else  {    stateQ(n);   }}    // Function for transition// state B       function stateB(n){      // length() of String  // become 0 then  // print Accepted  if (n.length == 0)  {    document.write("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.substr(1));       // If at index 0    // 'b' if found then    // call stateD function    // with passing n[1:] to it    else      stateD(n.substr(1));  }   }   // Function for transition// state Cfunction stateC(n){      // length() of String  // become 0 then  // print Accepted  if (n.length == 0)    document.write("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.substr(1));       // If at index 0    // 'b' if found then    // call stateQ function    // with passing n to it    else      stateQ(n);  }}   // Function for transition// state Dfunction stateD(n){      // length() of String  // become 0 then  // print Accepted  if (n.length() == 0)    document.write("Accepted");  else  {          // If at index 0    // 'a' if found then    // call stateC function    // with passing n[1:] to it    if (n[0] == 'a')    {      stateB(n.substr(1));    }       // If at index 0    // 'b' if found then    // call stateQ function    // with passing n to it    else    {      stateD(n.substr(1));    }  }}   // Function for state Q// transitionfunction stateQ(n){      // In dead state  // it shows String  // not accepted  document.write("Not Accepted");} // Driver code     // Take String input  let n ="aaaba";      // Call stateA to check the input  stateA(n);     // This code is contributed by sanjoy_62.</script>
Not Accepted
 
Time Complexity: O(N)  Auxiliary Space: O(N)
rutvik_56
ukasp
pratham76
sanjoy_62
ankita_saini
pankajsharmagfg
DFA
substring
Algorithms
Strings
Theory of Computation & Automata
Strings
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
How to Start Learning DSA?
K means Clustering - Introduction
Quadratic Probing in Hashing
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
Write a program to reverse an array or string
Reverse a string in Java
Longest Common Subsequence | DP-4
C++ Data Types
Write a program to print all permutations of a given string