Program to build DFA that starts and end with 'a' from input (a, b)
Last Updated :
18 Apr, 2023
DFA (Deterministic Finite Automaton or Acceptor) is a finite state machine that accepts or rejects strings of symbols. DFA accepts the string if it reaches the final state and rejects otherwise.
Now the problem is, provided a string as input character by character and we have to check whether the string starts and ends with 'a'. We can only store the current character, since there is no concept of memory and hence the DFA cannot store the string provided. Otherwise, we could have just checked the first and last character for this problem. The input set for this problem is (a, b).
We cannot store anything accept the current character, which make this program a little different and tough than other string related problems.
Examples:
Input : a b a b a
Output : Yes
Explanation : (a b a b a) starts and
end with 'a'
Input : a b a b b
Output : No
Explanation : (a b a b b) starts with
'a' but doesn't end with 'a'
We first build a DFA for this problem. Making DFA is like making a flowchart for this program and then implement it in any language. You should have the knowledge of DFA and Finite Automata.
The DFA for given problem is:

Implementation:
C++
// C++ Program to DFA that accept strings
// which starts and end with 'a' over input(a, b)
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
// for producing different random
// numbers every time.
srand(time(0));
// random length of string from 1 - 16
// we are taking input from input stream,
// we can take delimiter to end the string
int max = 1 + rand() % 15;
// generating random string and processing it
int i = 0;
while (i < max) {
// producing random character over
// input alphabet (a, b)
char c = 'a' + rand() % 2;
cout << c << " ";
i++;
// first character is 'a'
if (c == 'a') {
// if there is only 1 character
// i.e. 'a'
if (i == max)
cout << "YES\n";
while (i < max) {
c = 'a' + rand() % 2;
cout << c << " ";
i++;
// if character is 'a' and it
// is the last character
if (c == 'a' && i == max) {
cout << "\nYES\n";
}
// if character is 'b' and it
// is the last character
else if (i == max) {
cout << "\nNO\n";
}
}
}
// first character is 'b' so no matter
// what the string is, it is not going
// to be accepted
else {
while (i < max) {
c = 'a' + rand() % 2;
cout << c << " ";
i++;
}
cout << "\nNO\n";
}
}
return 0;
}
Java
// JAVA Program to DFA that accept Strings
// which starts and end with 'a' over input(a, b)
import java.util.*;
class GFG
{
public static void main(String[] args)
{
// for producing different random
// numbers every time.
Random r = new Random();
// random length of String from 1 - 16
// we are taking input from input stream,
// we can take delimiter to end the String
int max = 1 + r.nextInt()*10 % 15;
// generating random String and processing it
int i = 0;
while (i < max)
{
// producing random character over
// input alphabet (a, b)
char c = (char) ('a' + r.nextInt()*10 % 2);
System.out.print(c+ " ");
i++;
// first character is 'a'
if (c == 'a')
{
// if there is only 1 character
// i.e. 'a'
if (i == max)
System.out.print("YES\n");
while (i < max)
{
c = (char) ('a' + r.nextInt()*10 % 2);
System.out.print(c+ " ");
i++;
// if character is 'a' and it
// is the last character
if (c == 'a' && i == max)
{
System.out.print("\nYES\n");
}
// if character is 'b' and it
// is the last character
else if (i == max)
{
System.out.print("\nNO\n");
}
}
}
// first character is 'b' so no matter
// what the String is, it is not going
// to be accepted
else
{
while (i < max)
{
c = (char) ('a' + r.nextInt()*10 % 2);
System.out.print(c+ " ");
i++;
}
System.out.print("\nNO\n");
}
}
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python Program to DFA that accept Strings
# which starts and end with 'a' over input(a, b)
import random
# for producing different random
# numbers every time.
r = random.Random()
# random length of String from 1 - 16
# we are taking input from input stream,
# we can take delimiter to end the String
max = 1 + r.randint(0, 14)
# generating random String and processing it
i = 0
while i < max:
# producing random character over
# input alphabet (a, b)
c = chr(ord('a') + r.randint(0, 1))
print(c, end=" ")
i += 1
# first character is 'a'
if c == 'a':
# if there is only 1 character
# i.e. 'a'
if i == max:
print("YES")
while i < max:
c = chr(ord('a') + r.randint(0, 1))
print(c, end=" ")
i += 1
# if character is 'a' and it
# is the last character
if c == 'a' and i == max:
print("\nYES")
# if character is 'b' and it
# is the last character
elif i == max:
print("\nNO")
# first character is 'b' so no matter
# what the String is, it is not going
# to be accepted
else:
while i < max:
c = chr(ord('a') + r.randint(0, 1))
print(c, end=" ")
i += 1
print("\nNO")
# This code is contributed by codebraxznt
C#
// C# Program to DFA that accept Strings
// which starts and end with 'a' over i.Add(a, b)
using System;
class GFG
{
static void Main(String[] args)
{
// random length of String from 1 - 16
// we are taking input from input stream,
// we can take delimiter to end the String
int max = 1 + new Random().Next()*10 % 15;
// generating random String and processing it
int i = 0;
while (i < max)
{
// producing random character over
// input alphabet (a, b)
char c = (char) ('a' + new Random().Next()*10 % 2);
Console.Write(c + " ");
i++;
// first character is 'a'
if (c == 'a')
{
// if there is only 1 character
// i.e. 'a'
if (i == max)
Console.Write("YES\n");
while (i < max)
{
c = (char) ('a' + new Random().Next()*10 % 2);
Console.Write(c + " ");
i++;
// if character is 'a' and it
// is the last character
if (c == 'a' && i == max)
{
Console.Write("\nYES\n");
}
// if character is 'b' and it
// is the last character
else if (i == max)
{
Console.Write("\nNO\n");
}
}
}
// first character is 'b' so no matter
// what the String is, it is not going
// to be accepted
else
{
while (i < max)
{
c = (char) ('a' + new Random().Next()*10 % 2);
Console.Write(c + " ");
i++;
}
Console.Write("\nNO\n");
}
}
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to DFA that accept strings
// which starts and end with 'a' over input(a, b)
// Random length of String from 1 - 16
// we are taking input from input stream,
// we can take delimiter to end the String
let max = 1 + Math.floor(Math.random() * 10 % 15);
// Generating random String and processing it
let i = 0;
while (i < max)
{
// Producing random character over
// input alphabet (a, b)
let c = String.fromCharCode(
'a'.charCodeAt(0) +
Math.floor(Math.random() * 10) % 2);
document.write(c + " ");
i++;
// First character is 'a'
if (c == 'a')
{
// If there is only 1 character
// i.e. 'a'
if (i == max)
document.write("YES<br>");
while (i < max)
{
c = String.fromCharCode(
'a'.charCodeAt(0) +
Math.floor(Math.random() * 10) % 2);
document.write(c + " ");
i++;
// If character is 'a' and it
// is the last character
if (c == 'a' && i == max)
{
document.write("<br>YES<br>");
}
// If character is 'b' and it
// is the last character
else if (i == max)
{
document.write("<br>NO<br>");
}
}
}
// First character is 'b' so no matter
// what the String is, it is not going
// to be accepted
else
{
while (i < max)
{
c = String.fromCharCode(
'a'.charCodeAt(0) +
Math.floor(Math.random() * 10) % 2);
document.write(c + " ");
i++;
}
document.write("<br>NO<br>");
}
}
// This code is contributed by rag2127
</script>
Outputa b a b a b a b b b a b a
YES
Time Complexity: O(MAX)
Auxiliary Space: O(1)
Similar Reads
Program to build a DFA that accepts Strings ending with abc Given a string, str consists of characters 'a', 'b' & 'c', the task is to check whether string str ends with "abc" or not. If it does, print âAcceptedâ with state transitions, else print âNot Acceptedâ. Examples: Input: str = "cbabc"Output: AcceptedExplanation: c : q0-->q0b: q0-->q0a: q0--
11 min read
Program to build a DFA that checks if a string ends with "01" or "10" DFA or Deterministic Finite Automata is a finite state machine which accepts a string(under some specific condition) if it reaches a final state, otherwise rejects it.Problem: Given a string of '0's and '1's character by character, check for the last two characters to be "01" or "10" else reject the
10 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
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
DFA that begins with 'a' but does not contain substring 'aab' 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'.
10 min read