DFA for strings not containing consecutive two a's and starting with 'a'
Last Updated :
11 Jul, 2025
Prerequisite - Finite Automata Introduction
Problem: Construct deterministic finite automata (DFA) for strings not containing consecutive two a's and starting with a.
Explanation:
Accept Strings that not contain consecutive two a's. Check if a given string contain consecutive two a's or not. The any occurrence of (b-z) should not affect the scenario. Strings should follow this pattern:
a.(ww|(ww.a))*
where, ww = all possible character except a
All those strings that are not fall in the above mentioned pattern are not accepted.
Deterministic finite automata (DFA) of strings that not contain consecutive two a's given as following below. The initial and starting state in this dfa is q0.

Approach used:
In this program, consider the 6 states to be 0, 1, 2, 3, 4 and 5. Now let us take a variable named DFA which will be initially 0. Whenever any transition takes place, it will update the value of DFA with the number associated with new state.
Example:
If a transition occurs from state 0 to state 1 then the value of DFA will be updated to 1. If a transition occurs from state 2 to state 3 then the value of dfa will be updated to 3. In this way, apply this algorithm on entire string and if in the end, then reach state 1, 2, 4 or 5 then our string will be accepted otherwise not.
Input : geeksaforaageeks
Output : NOT ACCEPTED
Input : aageeksforageeks
Output : NOT ACCEPTED
Input : ageeksaforageeks
Output : ACCEPTED
Implementation:
C++
// C++ program to implement DFA for Strings
// not containing consecutive two a's
#include <bits/stdc++.h>
using namespace std;
int dfa = 0;
// This function is for
// the starting state of DFA
void start(char c)
{
if (c == 'a')
{
dfa = 1;
}
else
{
dfa = 3;
}
}
// This function is for the first state of DFA
void state1(char c)
{
if (c == 'a')
{
dfa = 3;
}
else
{
dfa = 4;
}
}
// This function is for the second state of DFA
void state2(char c)
{
if (c == 'a')
{
dfa = 5;
}
else
{
dfa = 2;
}
}
// This function is for the third state of DFA
void state3(char c)
{
if (c == 'a')
{
dfa = 3;
}
else
{
dfa = 3;
}
}
// This function is for the fourth state of DFA
void state4(char c)
{
if (c == 'a')
{
dfa = 5;
}
else
{
dfa = 2;
}
}
void state5(char c)
{
if (c == 'a')
{
dfa = 3;
}
else
{
dfa = 2;
}
}
int isAccepted(char str[])
{
// Store length of string
int i, len = strlen(str);
for(i = 0; i < len; i++)
{
// cout<<"%d", dfa);
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else if (dfa == 3)
state3(str[i]);
else if (dfa == 4)
state4(str[i]);
else if (dfa == 5)
state5(str[i]);
else
return 0;
}
if (dfa == 0)
return 0;
else if (dfa == 3)
return 0;
else
return 1;
}
// Driver code
int main()
{
char str[] = "ageeksaforageeks";
if (isAccepted(str))
cout << "ACCEPTED";
else
cout << "NOT ACCEPTED";
return 0;
}
// This code is contributed by shivanisinghss2110.
C
// C program to implement DFA for Strings
// not containing consecutive two a's
#include <stdio.h>
#include <string.h>
int dfa = 0;
// This function is for
// the starting state of DFA
void start(char c)
{
if (c == 'a') {
dfa = 1;
}
else {
dfa = 3;
}
}
// This function is for the first state of DFA
void state1(char c)
{
if (c == 'a') {
dfa = 3;
}
else {
dfa = 4;
}
}
// This function is for the second state of DFA
void state2(char c)
{
if (c == 'a') {
dfa = 5;
}
else {
dfa = 2;
}
}
// This function is for the third state of DFA
void state3(char c)
{
if (c == 'a') {
dfa = 3;
}
else {
dfa = 3;
}
}
// This function is for the fourth state of DFA
void state4(char c)
{
if (c == 'a') {
dfa = 5;
}
else {
dfa = 2;
}
}
void state5(char c)
{
if (c == 'a') {
dfa = 3;
}
else {
dfa = 2;
}
}
int isAccepted(char str[])
{
// store length of string
int i, len = strlen(str);
for (i = 0; i < len; i++) {
// printf("%d", dfa);
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else if (dfa == 3)
state3(str[i]);
else if (dfa == 4)
state4(str[i]);
else if (dfa == 5)
state5(str[i]);
else
return 0;
}
if (dfa == 0)
return 0;
else if (dfa == 3)
return 0;
else
return 1;
}
// driver code
int main()
{
char str[] = "ageeksaforageeks";
if (isAccepted(str))
printf("ACCEPTED");
else
printf("NOT ACCEPTED");
return 0;
}
// This code is contributed by SHUBHAMSINGH10.
Java
// Java program to implement DFA for Strings
// not containing consecutive two a's
import java.util.*;
class GFG
{
static int dfa = 0;
// This function is for
// the starting state of DFA
static void start(char c)
{
if (c == 'a')
{
dfa = 1;
}
else
{
dfa = 3;
}
}
// This function is for the first state of DFA
static void state1(char c)
{
if (c == 'a')
{
dfa = 3;
}
else
{
dfa = 4;
}
}
// This function is for the second state of DFA
static void state2(char c)
{
if (c == 'a')
{
dfa = 5;
}
else
{
dfa = 2;
}
}
// This function is for the third state of DFA
static void state3(char c)
{
if (c == 'a')
{
dfa = 3;
}
else
{
dfa = 3;
}
}
// This function is for the fourth state of DFA
static void state4(char c)
{
if (c == 'a')
{
dfa = 5;
}
else
{
dfa = 2;
}
}
static void state5(char c)
{
if (c == 'a')
{
dfa = 3;
}
else
{
dfa = 2;
}
}
static int isAccepted(char str[])
{
// store length of string
int i, len = str.length;
for (i = 0; i < len; i++)
{
// System.out.printf("%d", dfa);
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else if (dfa == 3)
state3(str[i]);
else if (dfa == 4)
state4(str[i]);
else if (dfa == 5)
state5(str[i]);
else
return 0;
}
if (dfa == 0)
return 0;
else if (dfa == 3)
return 0;
else
return 1;
}
// Driver code
public static void main(String args[])
{
char str[] = "ageeksaforageeks".toCharArray();
if (isAccepted(str) == 1)
System.out.printf("ACCEPTED");
else
System.out.printf("NOT ACCEPTED");
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to implement DFA for Strings
# not containing consecutive two a's
# This function is for the dfa = starting
# dfa = state (zeroth) of DFA
def start(c):
if (c == 'a'):
dfa = 1
else:
dfa = 3
return dfa
# This function is for the first
# dfa = state of DFA
def state1(c):
if (c == 'a'):
dfa = 3
else:
dfa = 4
return dfa
# This function is for the second
# dfa = state of DFA
def state2(c):
if (c == 'a'):
dfa = 5
else:
dfa = 2
return dfa
# This function is for the third
# dfa = state of DFA
def state3(c):
if (c == 'a'):
dfa = 3
else:
dfa = 3
return dfa
# This function is for the fourth
# dfa = state of DFA
def state4(c):
if (c == 'a'):
dfa = 5
else:
dfa = 2
return dfa
# This function is for the fifth
# dfa = state of DFA
def state5(c):
if (c == 'a'):
dfa = 3
else:
dfa = 2
return dfa
def isAccepted(String):
# store length of Stringing
l = len(String)
# dfa tells the number associated
# with the present dfa = state
dfa = 0
for i in range(l):
if (dfa == 0):
dfa = start(String[i])
elif (dfa == 1):
dfa = state1(String[i])
elif (dfa == 2) :
dfa = state2(String[i])
elif (dfa == 3) :
dfa = state3(String[i])
elif (dfa == 4) :
dfa = state4(String[i])
elif (dfa == 5) :
dfa = state5(String[i])
else:
return 0
if(dfa == 3 or dfa == 0) :
return 0
else:
return 1
# Driver code
if __name__ == "__main__" :
String = "ageeksaforageeks"
if (isAccepted(String)) :
print("ACCEPTED")
else:
print("NOT ACCEPTED")
# This code is contributed by SHUBHAMSINGH10.
C#
// C# program to implement DFA for Strings
// not containing consecutive two a's
using System;
public class GFG
{
static int dfa = 0;
// This function is for
// the starting state of DFA
static void start(char c)
{
if (c == 'a')
{
dfa = 1;
}
else
{
dfa = 3;
}
}
// This function is for the first state of DFA
static void state1(char c)
{
if (c == 'a')
{
dfa = 3;
}
else
{
dfa = 4;
}
}
// This function is for the second state of DFA
static void state2(char c)
{
if (c == 'a')
{
dfa = 5;
}
else
{
dfa = 2;
}
}
// This function is for the third state of DFA
static void state3(char c)
{
if (c == 'a')
{
dfa = 3;
}
else
{
dfa = 3;
}
}
// This function is for the fourth state of DFA
static void state4(char c)
{
if (c == 'a')
{
dfa = 5;
}
else
{
dfa = 2;
}
}
static void state5(char c)
{
if (c == 'a')
{
dfa = 3;
}
else
{
dfa = 2;
}
}
static int isAccepted(char[] str)
{
// store length of string
int i, len = str.Length;
for (i = 0; i < len; i++)
{
// System.out.printf("%d", dfa);
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else if (dfa == 3)
state3(str[i]);
else if (dfa == 4)
state4(str[i]);
else if (dfa == 5)
state5(str[i]);
else
return 0;
}
if (dfa == 0)
return 0;
else if (dfa == 3)
return 0;
else
return 1;
}
// Driver code
public static void Main(String []args)
{
char []str = "ageeksaforageeks".ToCharArray();
if (isAccepted(str) == 1)
Console.WriteLine("ACCEPTED");
else
Console.WriteLine("NOT ACCEPTED");
}
}
/* This code contributed by PrinciRaj1992 */
JavaScript
<script>
// JavaScript program to implement DFA
// for Strings not containing consecutive
// two a's
var dfa = 0;
// This function is for
// the starting state of DFA
function start(c)
{
if (c === "a")
{
dfa = 1;
}
else
{
dfa = 3;
}
}
// This function is for the first state of DFA
function state1(c)
{
if (c === "a")
{
dfa = 3;
}
else
{
dfa = 4;
}
}
// This function is for the second state of DFA
function state2(c)
{
if (c === "a")
{
dfa = 5;
}
else
{
dfa = 2;
}
}
// This function is for the third state of DFA
function state3(c)
{
if (c === "a")
{
dfa = 3;
}
else
{
dfa = 3;
}
}
// This function is for the fourth state of DFA
function state4(c)
{
if (c === "a")
{
dfa = 5;
}
else
{
dfa = 2;
}
}
function state5(c)
{
if (c === "a")
{
dfa = 3;
}
else
{
dfa = 2;
}
}
function isAccepted(str)
{
// Store length of string
var i,
len = str.length;
for(i = 0; i < len; i++)
{
// System.out.printf("%d", dfa);
if (dfa === 0) start(str[i]);
else if (dfa === 1) state1(str[i]);
else if (dfa === 2) state2(str[i]);
else if (dfa === 3) state3(str[i]);
else if (dfa === 4) state4(str[i]);
else if (dfa === 5) state5(str[i]);
else return 0;
}
if (dfa === 0) return 0;
else if (dfa === 3) return 0;
else return 1;
}
// Driver code
var str = "ageeksaforageeks".split("");
if (isAccepted(str) === 1)
document.write("ACCEPTED");
else
document.write("NOT ACCEPTED");
// This code is contributed by rdtank
</script>
Output:
ACCEPTED
The time complexity of this program is O(n)
Auxiliary space: O(n) because it is using constant space for variables
Similar Reads
Program to build a DFA that accepts strings starting and ending with different character Prerequisite: Deterministic Finite Automata Given a string, str consists of characters 'a' & 'b'. The task is to check whether string str starts and ends with different characters or not. If it does, print 'YES' with state transitions, else print 'NO'. Examples: Input: ababab Output: YES Explana
12 min read
Construct a DFA that Start With aa or bb Prerequisite: Designing Finite Automata DFA (Deterministic Finite Automata or Acceptor) is a finite state machine that accepts or rejects strings of symbols. DFA accepts the string if it reaches the final state otherwise it rejects it. In these types of problems, we have some given parameters accord
2 min read
Program to build a DFA to accept strings that start and end with same character Given a string consisting of characters a and b, check if the string starts and ends with the same character or not. If it does, print 'Yes' else print 'No'.Examples: Input: str = "abbaaba" Output: Yes Explanation: The given input string starts and ends with same character 'a' So the states of the b
12 min read
DFA for Strings not ending with "THE" Problem - Accept Strings that not ending with substring "THE". Check if a given string is ending with "the" or not. The different forms of "the" which are avoided in the end of the string are: "THE", "ThE", "THe", "tHE", "thE", "The", "tHe" and "the" All those strings that are ending with any of the
12 min read
LEX Code which accepts string containing third last element âaâ over input alphabet {a, b} In this article, we will discuss the DFA in LEX Code which accepts a string containing the third-last element âaâ over input alphabet {a, b} with the help of example. Let's discuss it one y one. Pre-requisite - Designing Finite Automata Problem Overview : Design a DFA in LEX Code which accepts a str
2 min read
Construct DFA with Σ = {a, b} and Accept All String of Length at Least 2 Construct a DFA for a language accepting strings of length at least two, over input alphabets Σ = {a, b}. This means that in DFA, language consists of a string of length of at least 2 and can be greater than two. That means if DFA got the string of Length 0 or 1 then it will not accept it. A string
5 min read