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:
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 DFA diagram:

Below is the implementation of the above DFA:
C++
#include <bits/stdc++.h>
using namespace std;
void stateQ(string);
void stateA(string);
void stateB(string);
void stateC(string);
void stateD(string);
void stateQ(string n)
{
cout << ( "Not Accepted" );
}
void stateA(string n)
{
if (n[0] == 'a' )
stateB(n.substr(
1, n.length() + 1));
else
stateQ(n);
}
void stateB(string n)
{
if (n.length() == 0)
cout << ( "Accepted" );
else
{
if (n[0] == 'a' )
stateC(n.substr(
1, n.length() - 1));
else
stateD(n.substr(
1, n.length() - 1));
}
}
void stateC(string n)
{
if (n.length() == 0)
cout << ( "Accepted" );
else
{
if (n[0] == 'a' )
stateC(n.substr(
1, n.length() + 1));
else
stateQ(n);
}
}
void stateD(string n)
{
if (n.length() == 0)
cout << ( "Accepted" );
else
{
if (n[0] == 'a' )
stateB(n.substr(
1, n.length() + 1));
else
stateD(n.substr(
1, n.length() + 1));
}
}
int main()
{
string n = "aaaba" ;
stateA(n);
}
|
Java
import java.util.*;
class GFG{
static void stateA(String n)
{
if (n.charAt( 0 ) == 'a' )
{
stateB(n.substring( 1 ));
}
else
{
stateQ(n);
}
}
static void stateB(String n)
{
if (n.length() == 0 )
{
System.out.print( "Accepted" );
}
else
{
if (n.charAt( 0 ) == 'a' )
stateC(n.substring( 1 ));
else
stateD(n.substring( 1 ));
}
}
static void stateC(String n)
{
if (n.length() == 0 )
System.out.print( "Accepted" );
else
{
if (n.charAt( 0 ) == 'a' )
stateC(n.substring( 1 ));
else
stateQ(n);
}
}
static void stateD(String n)
{
if (n.length() == 0 )
System.out.print( "Accepted" );
else
{
if (n.charAt( 0 ) == 'a' )
{
stateB(n.substring( 1 ));
}
else
{
stateD(n.substring( 1 ));
}
}
}
static void stateQ(String n)
{
System.out.print( "Not Accepted" );
}
public static void main(String []args)
{
String n = "aaaba" ;
stateA(n);
}
}
|
Python3
def stateA(n):
if (n[ 0 ] = = 'a' ):
stateB(n[ 1 :])
else :
stateQ(n)
def stateB(n):
if ( len (n) = = 0 ):
print ( "Accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateC(n[ 1 :])
else :
stateD(n[ 1 :])
def stateC(n):
if ( len (n) = = 0 ):
print ( "Accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateC(n[ 1 :])
else :
stateQ(n)
def stateD(n):
if ( len (n) = = 0 ):
print ( "Accepted" )
else :
if (n[ 0 ] = = 'a' ):
stateB(n[ 1 :])
else :
stateD(n[ 1 :])
def stateQ(n):
print ( "Not Accepted" )
n = "aaaba"
stateA(n)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static void stateA( string n)
{
if (n[0] == 'a' )
{
stateB(n.Substring(1));
}
else
{
stateQ(n);
}
}
static void stateB( string n)
{
if (n.Length == 0)
{
Console.Write( "Accepted" );
}
else
{
if (n[0] == 'a' )
stateC(n.Substring(1));
else
stateD(n.Substring(1));
}
}
static void stateC( string n)
{
if (n.Length == 0)
Console.Write( "Accepted" );
else
{
if (n[0] == 'a' )
stateC(n.Substring(1));
else
stateQ(n);
}
}
static void stateD( string n)
{
if (n.Length == 0)
Console.Write( "Accepted" );
else
{
if (n[0] == 'a' )
{
stateB(n.Substring(1));
}
else
{
stateD(n.Substring(1));
}
}
}
static void stateQ( string n)
{
Console.Write( "Not Accepted" );
}
public static void Main( string []args)
{
string n = "aaaba" ;
stateA(n);
}
}
|
Javascript
<script>
function stateA(n)
{
if (n[0] == 'a' )
{
stateB(n.substr(1));
}
else
{
stateQ(n);
}
}
function stateB(n)
{
if (n.length == 0)
{
document.write( "Accepted" );
}
else
{
if (n[0] == 'a' )
stateC(n.substr(1));
else
stateD(n.substr(1));
}
}
function stateC(n)
{
if (n.length == 0)
document.write( "Accepted" );
else
{
if (n[0] == 'a' )
stateC(n.substr(1));
else
stateQ(n);
}
}
function stateD(n)
{
if (n.length() == 0)
document.write( "Accepted" );
else
{
if (n[0] == 'a' )
{
stateB(n.substr(1));
}
else
{
stateD(n.substr(1));
}
}
}
function stateQ(n)
{
document.write( "Not Accepted" );
}
let n = "aaaba" ;
stateA(n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)