DFA for Strings not ending with “THE”
Last Updated :
20 Dec, 2022
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 above mentioned forms of “the” are not accepted.
Deterministic finite automata (DFA) of strings that not ending with “THE” –
The initial and starting state in this dfa is Qo
Approach used in the program –
In this program, consider the 4 states to be 0, 1, 2 and 3. 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 0, 1 or 2 then our string will be accepted otherwise not.
Input : XYzabCthe
Output : NOT ACCEPTED
Input : Themaliktth
Output : ACCEPTED
C++
#include <iostream>
using namespace std;
int dfa = 0;
void start( char c)
{
if (c == 't' || c == 'T' )
dfa = 1;
}
void state1( char c)
{
if (c == 't' || c == 'T' )
dfa = 1;
else if (c == 'h' || c == 'H' )
dfa = 2;
else
dfa = 0;
}
void state2( char c)
{
if (c == 'e' || c == 'E' )
dfa = 3;
else if (c == 't' || c == 'T' )
dfa = 1;
else
dfa = 0;
}
void state3( char c)
{
if (c == 't' || c == 'T' )
dfa = 1;
else
dfa = 0;
}
bool isAccepted(string str)
{
int len = str.length();
for ( int i = 0; i < len; i++)
{
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else
state3(str[i]);
}
return (dfa != 3);
}
int main()
{
string str = "forTHEgeeks" ;
if (isAccepted(str) == true )
cout << "ACCEPTED\n" ;
else
cout << "NOT ACCEPTED\n" ;
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
int dfa = 0;
void start( char c)
{
if (c == 't' || c == 'T' )
dfa = 1;
}
void state1( char c)
{
if (c == 't' || c == 'T' )
dfa = 1;
else if (c == 'h' || c == 'H' )
dfa = 2;
else
dfa = 0;
}
void state2( char c)
{
if (c == 'e' || c == 'E' )
dfa = 3;
else if (c == 't' || c == 'T' )
dfa = 1;
else
dfa = 0;
}
void state3( char c)
{
if (c == 't' || c == 'T' )
dfa = 1;
else
dfa = 0;
}
bool isAccepted( char str[])
{
int len = strlen (str);
for ( int i=0; i < len; i++) {
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else
state3(str[i]);
}
return (dfa != 3);
}
int main()
{
char str[] = "forTHEgeeks" ;
if (isAccepted(str) == true )
printf ( "ACCEPTED\n" );
else
printf ( "NOT ACCEPTED\n" );
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int dfa = 0 ;
static void start( char c)
{
if (c == 't' || c == 'T' )
dfa = 1 ;
}
static void state1( char c)
{
if (c == 't' || c == 'T' )
dfa = 1 ;
else if (c == 'h' || c == 'H' )
dfa = 2 ;
else
dfa = 0 ;
}
static void state2( char c)
{
if (c == 'e' || c == 'E' )
dfa = 3 ;
else
dfa = 0 ;
}
static void state3( char c)
{
if (c == 't' || c == 'T' )
dfa = 1 ;
else
dfa = 0 ;
}
static boolean isAccepted( char str[])
{
int len = str.length;
for ( int i= 0 ; i < len; i++)
{
if (dfa == 0 )
start(str[i]);
else if (dfa == 1 )
state1(str[i]);
else if (dfa == 2 )
state2(str[i]);
else
state3(str[i]);
}
return (dfa != 3 );
}
public static void main(String[] args)
{
char str[] = "forTHEgeeks" .toCharArray();
if (isAccepted(str) == true )
System.out.println( "ACCEPTED\n" );
else
System.out.println( "NOT ACCEPTED\n" );
}
}
|
Python3
def start(c):
if (c = = 't' or c = = 'T' ):
dfa = 1
def state1(c):
if (c = = 't' or c = = 'T' ):
dfa = 1
elif (c = = 'h' or c = = 'H' ):
dfa = 2
else :
dfa = 0
def state2(c):
if (c = = 'e' or c = = 'E' ):
dfa = 3
else :
dfa = 0
def state3(c):
if (c = = 't' or c = = 'T' ):
dfa = 1
else :
dfa = 0
def isAccepted(string):
length = len (string)
for i in range (length):
if (dfa = = 0 ):
start(string[i])
elif (dfa = = 1 ):
state1(string[i])
elif (dfa = = 2 ):
state2(string[i])
else :
state3(string[i])
return (dfa ! = 3 )
if __name__ = = "__main__" :
string = "forTHEgeeks"
dfa = 0
if isAccepted(string):
print ( "ACCEPTED" )
else :
print ( "NOT ACCEPTED" )
|
C#
using System;
class GFG
{
static int dfa = 0;
static void start( char c)
{
if (c == 't' || c == 'T' )
dfa = 1;
}
static void state1( char c)
{
if (c == 't' || c == 'T' )
dfa = 1;
else if (c == 'h' || c == 'H' )
dfa = 2;
else
dfa = 0;
}
static void state2( char c)
{
if (c == 'e' || c == 'E' )
dfa = 3;
else
dfa = 0;
}
static void state3( char c)
{
if (c == 't' || c == 'T' )
dfa = 1;
else
dfa = 0;
}
static bool isAccepted( char []str)
{
int len = str.Length;
for ( int i=0; i < len; i++)
{
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else
state3(str[i]);
}
return (dfa != 3);
}
static public void Main ()
{
char []str = "forTHEgeeks" .ToCharArray();
if (isAccepted(str) == true )
Console.WriteLine( "ACCEPTED\n" );
else
Console.WriteLine( "NOT ACCEPTED\n" );
}
}
|
PHP
<?php
$dfa = 0;
function start( $c )
{
global $dfa ;
if ( $c == 't' || $c == 'T' )
$dfa = 1;
}
function state1( $c )
{
global $dfa ;
if ( $c == 't' || $c == 'T' )
$dfa = 1;
else if ( $c == 'h' || $c == 'H' )
$dfa = 2;
else
$dfa = 0;
}
function state2( $c )
{
global $dfa ;
if ( $c == 'e' || $c == 'E' )
$dfa = 3;
else
$dfa = 0;
}
function state3( $c )
{
global $dfa ;
if ( $c == 't' || $c == 'T' )
$dfa = 1;
else
$dfa = 0;
}
function isAccepted( $str )
{
global $dfa ;
$len = strlen ( $str );
for ( $i =0; $i < $len ; $i ++)
{
if ( $dfa == 0)
start( $str [ $i ]);
else if ( $dfa == 1)
state1( $str [ $i ]);
else if ( $dfa == 2)
state2( $str [ $i ]);
else
state3( $str [ $i ]);
}
return ( $dfa != 3);
}
$str = "forTHEgeeks" ;
if (isAccepted( $str ) == true)
echo "ACCEPTED\n" ;
else
echo "NOT ACCEPTED\n" ;
?>
|
Javascript
<script>
let dfa = 0;
function start(c)
{
if (c == 't' || c == 'T' )
dfa = 1;
}
function state1(c)
{
if (c == 't' || c == 'T' )
dfa = 1;
else if (c == 'h' || c == 'H' )
dfa = 2;
else
dfa = 0;
}
function state2(c)
{
if (c == 'e' || c == 'E' )
dfa = 3;
else
dfa = 0;
}
function state3(c)
{
if (c == 't' || c == 'T' )
dfa = 1;
else
dfa = 0;
}
function isAccepted(str)
{
let len = str.length;
for (let i=0; i < len; i++)
{
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else
state3(str[i]);
}
return (dfa != 3);
}
let str = "forTHEgeeks" .split( '' );
if (isAccepted(str) == true )
document.write( "ACCEPTED" );
else
document.write( "NOT ACCEPTED" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1) since it is using constant space