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++
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand ( time (0));
int max = 1 + rand () % 15;
int i = 0;
while (i < max) {
char c = 'a' + rand () % 2;
cout << c << " " ;
i++;
if (c == 'a' ) {
if (i == max)
cout << "YES\n" ;
while (i < max) {
c = 'a' + rand () % 2;
cout << c << " " ;
i++;
if (c == 'a' && i == max) {
cout << "\nYES\n" ;
}
else if (i == max) {
cout << "\nNO\n" ;
}
}
}
else {
while (i < max) {
c = 'a' + rand () % 2;
cout << c << " " ;
i++;
}
cout << "\nNO\n" ;
}
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void main(String[] args)
{
Random r = new Random();
int max = 1 + r.nextInt()* 10 % 15 ;
int i = 0 ;
while (i < max)
{
char c = ( char ) ( 'a' + r.nextInt()* 10 % 2 );
System.out.print(c+ " " );
i++;
if (c == '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 (c == 'a' && i == max)
{
System.out.print( "\nYES\n" );
}
else if (i == max)
{
System.out.print( "\nNO\n" );
}
}
}
else
{
while (i < max)
{
c = ( char ) ( 'a' + r.nextInt()* 10 % 2 );
System.out.print(c+ " " );
i++;
}
System.out.print( "\nNO\n" );
}
}
}
}
|
Python3
import random
r = random.Random()
max = 1 + r.randint( 0 , 14 )
i = 0
while i < max :
c = chr ( ord ( 'a' ) + r.randint( 0 , 1 ))
print (c, end = " " )
i + = 1
if c = = 'a' :
if i = = max :
print ( "YES" )
while i < max :
c = chr ( ord ( 'a' ) + r.randint( 0 , 1 ))
print (c, end = " " )
i + = 1
if c = = 'a' and i = = max :
print ( "\nYES" )
elif i = = max :
print ( "\nNO" )
else :
while i < max :
c = chr ( ord ( 'a' ) + r.randint( 0 , 1 ))
print (c, end = " " )
i + = 1
print ( "\nNO" )
|
C#
using System;
class GFG
{
static void Main(String[] args)
{
int max = 1 + new Random().Next()*10 % 15;
int i = 0;
while (i < max)
{
char c = ( char ) ( 'a' + new Random().Next()*10 % 2);
Console.Write(c + " " );
i++;
if (c == 'a' )
{
if (i == max)
Console.Write( "YES\n" );
while (i < max)
{
c = ( char ) ( 'a' + new Random().Next()*10 % 2);
Console.Write(c + " " );
i++;
if (c == 'a' && i == max)
{
Console.Write( "\nYES\n" );
}
else if (i == max)
{
Console.Write( "\nNO\n" );
}
}
}
else
{
while (i < max)
{
c = ( char ) ( 'a' + new Random().Next()*10 % 2);
Console.Write(c + " " );
i++;
}
Console.Write( "\nNO\n" );
}
}
}
}
|
Javascript
<script>
let max = 1 + Math.floor(Math.random() * 10 % 15);
let i = 0;
while (i < max)
{
let c = String.fromCharCode(
'a' .charCodeAt(0) +
Math.floor(Math.random() * 10) % 2);
document.write(c + " " );
i++;
if (c == '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 (c == 'a' && i == max)
{
document.write( "<br>YES<br>" );
}
else if (i == max)
{
document.write( "<br>NO<br>" );
}
}
}
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>" );
}
}
</script>
|
Output
a b a b a b a b b b a b a
YES
Time Complexity: O(MAX)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...