The Wayback Machine - https://web.archive.org/web/20240607080328/https://www.geeksforgeeks.org/dfa-for-strings-not-ending-with-the/
Open In App

DFA for Strings not ending with “THE”

Last Updated : 20 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to implement DFS that accepts
// all string that do not end with "THE"
#include <iostream>
using namespace std;
 
// dfa tells the number associated
// with the present state
int dfa = 0;
 
// This function is for
// the starting state (zeroth) of DFA
void start(char c)
{
     
    // On receiving 'T' or 't' goto
    // first state (1)
    if (c == 't' || c == 'T')
        dfa = 1;
}
 
// This function is for the first state of DFA
void state1(char c)
{
     
    // On receiving 'T' or 't' goto
    // first state (1)
    if (c == 't' || c == 'T')
        dfa = 1;
 
    // On receiving 'H' or 'h'
    // goto second state (2)
    else if (c == 'h' || c == 'H')
        dfa = 2;
 
    // Else goto starting state (0)
    else
        dfa = 0;
}
 
// This function is for the second state of DFA
void state2(char c)
{
     
    // On receiving 'E' or 'e' goto third state (3)
    // else goto starting state (0)
    if (c == 'e' || c == 'E')
        dfa = 3;
    else if (c == 't' || c == 'T')
        dfa = 1;
      else
        dfa = 0;
}
 
// This function is for the third state of DFA
void state3(char c)
{
     
    // On receiving 'T' or 't' goto first state (1)
    // else goto starting state (0)
    if (c == 't' || c == 'T')
        dfa = 1;
    else
        dfa = 0;
}
 
bool isAccepted(string str)
{
     
    // Store length of string
    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);
}
 
// Driver code
int main()
{
    string str = "forTHEgeeks";
    if (isAccepted(str) == true)
        cout << "ACCEPTED\n";
    else
        cout << "NOT ACCEPTED\n";
         
    return 0;
}
 
// This code is contributed by ShubhamSingh10


C




// C program to implement DFS that accepts
// all string that do not end with "THE"
#include <stdio.h>
#include <string.h>
 
// dfa tells the number associated
// with the present state
int dfa = 0;
 
// This function is for
// the starting state (zeroth) of DFA
void start(char c)
{
    // On receiving 'T' or 't' goto first state (1)
    if (c == 't' || c == 'T')
        dfa = 1;
}
 
// This function is for the first state of DFA
void state1(char c)
{
    // On receiving 'T' or 't' goto first state (1)
    if (c == 't' || c == 'T')
        dfa = 1;
 
    // On receiving 'H' or 'h' goto second state (2)
    else if (c == 'h' || c == 'H')
        dfa = 2;
 
    // else goto starting state (0)
    else
        dfa = 0;
}
 
// This function is for the second state of DFA
void state2(char c)
{
    // On receiving 'E' or 'e' goto third state (3)
    // else goto starting state (0)
    if (c == 'e' || c == 'E')
        dfa = 3;
    else if (c == 't' || c == 'T')
        dfa = 1;
      else
        dfa = 0;
}
 
// This function is for the third state of DFA
void state3(char c)
{
    // On receiving 'T' or 't' goto first state (1)
    // else goto starting state (0)
    if (c == 't' || c == 'T')
        dfa = 1;
    else
        dfa = 0;
}
 
bool isAccepted(char str[])
{
    // store length of string
    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);
}
 
// driver code
int main()
{
    char str[] = "forTHEgeeks";
    if (isAccepted(str) == true)
        printf("ACCEPTED\n");
    else
        printf("NOT ACCEPTED\n");
    return 0;
}


Java




// Java program to implement DFS that accepts
// all string that do not end with "THE"
import java.util.*;
 
class GFG
{
 
// dfa tells the number associated
// with the present state
static int dfa = 0;
 
// This function is for
// the starting state (zeroth) of DFA
static void start(char c)
{
    // On receiving 'T' or 't' goto first state (1)
    if (c == 't' || c == 'T')
        dfa = 1;
}
 
// This function is for the first state of DFA
static void state1(char c)
{
    // On receiving 'T' or 't' goto first state (1)
    if (c == 't' || c == 'T')
        dfa = 1;
 
    // On receiving 'H' or 'h' goto second state (2)
    else if (c == 'h' || c == 'H')
        dfa = 2;
 
    // else goto starting state (0)
    else
        dfa = 0;
}
 
// This function is for the second state of DFA
static void state2(char c)
{
    // On receiving 'E' or 'e' goto third state (3)
    // else goto starting state (0)
    if (c == 'e' || c == 'E')
        dfa = 3;
    else
        dfa = 0;
}
 
// This function is for the third state of DFA
static void state3(char c)
{
    // On receiving 'T' or 't' goto first state (1)
    // else goto starting state (0)
    if (c == 't' || c == 'T')
        dfa = 1;
    else
        dfa = 0;
}
 
static boolean isAccepted(char str[])
{
    // store length of string
    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);
}
 
// Driver code
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");
}
}
 
/* This code is contributed by PrinciRaj1992 */


Python3




# Python3 program to implement DFS that accepts
# all string that do not end with "THE"
 
# This function is for the starting
# state (zeroth) of DFA
def start(c):
     
    # On receiving 'T' or 't' goto
    # first state (1)
    if (c == 't' or c == 'T'):
        dfa=1
 
# This function is for the first state of DFA
def state1(c):
     
    # On receiving 'T' or 't' goto first state (1)
    if (c == 't' or c == 'T'):
        dfa = 1
 
    # On receiving 'H' or 'h' goto second state (2)
    elif (c == 'h' or c == 'H'):
        dfa = 2
 
    # else goto starting state (0)
    else:
        dfa = 0
 
# This function is for the second state of DFA
def state2(c):
     
    # On receiving 'E' or 'e' goto third
    # state (3) else goto starting state (0)
    if (c == 'e' or c == 'E'):
        dfa = 3
    else:
        dfa = 0
         
# This function is for the third state of DFA
def state3(c):
     
    # On receiving 'T' or 't' goto first
    # state (1) else goto starting state (0)
    if (c == 't' or c == 'T'):
        dfa = 1
    else:
        dfa = 0
 
def isAccepted(string):
     
    # store length of 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)
 
# Driver Code
if __name__ == "__main__" :
    string="forTHEgeeks"
     
    # dfa tells the number associated
    # with the present state
    dfa = 0
    if isAccepted(string):
        print("ACCEPTED")
    else:
        print("NOT ACCEPTED")
 
# This code is contributed by SHUBHAMSINGH10


C#




// C# program to implement DFS that accepts
// all string that do not end with "THE"
using System;
 
class GFG
{
     
// dfa tells the number associated
// with the present state
static int dfa = 0;
 
// This function is for
// the starting state (zeroth) of DFA
static void start(char c)
{
    // On receiving 'T' or 't' goto first state (1)
    if (c == 't' || c == 'T')
        dfa = 1;
}
 
// This function is for the first state of DFA
static void state1(char c)
{
    // On receiving 'T' or 't' goto first state (1)
    if (c == 't' || c == 'T')
        dfa = 1;
 
    // On receiving 'H' or 'h' goto second state (2)
    else if (c == 'h' || c == 'H')
        dfa = 2;
 
    // else goto starting state (0)
    else
        dfa = 0;
}
 
// This function is for the second state of DFA
static void state2(char c)
{
    // On receiving 'E' or 'e' goto third state (3)
    // else goto starting state (0)
    if (c == 'e' || c == 'E')
        dfa = 3;
    else
        dfa = 0;
}
 
// This function is for the third state of DFA
static void state3(char c)
{
    // On receiving 'T' or 't' goto first state (1)
    // else goto starting state (0)
    if (c == 't' || c == 'T')
        dfa = 1;
    else
        dfa = 0;
}
 
static bool isAccepted(char []str)
{
    // store length of string
    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);
}
 
// Driver code
static public void Main ()
{
    char []str = "forTHEgeeks".ToCharArray();
    if (isAccepted(str) == true)
        Console.WriteLine("ACCEPTED\n");
    else
        Console.WriteLine("NOT ACCEPTED\n");
}
}
 
/* This code is contributed by ajit. */


PHP




<?php
// PHP program to implement DFS
// that accepts all string that
// do not end with "THE"
 
// dfa tells the number associated
// with the present state
$dfa = 0;
 
// This function is for the
// starting state (zeroth) of DFA
function start($c)
{
    global $dfa;
    // On receiving 'T' or 't'
    // goto first state (1)
    if ($c == 't' || $c == 'T')
        $dfa = 1;
}
 
// This function is for
// the first state of DFA
function state1($c)
{
    global $dfa;
    // On receiving 'T' or 't'
    // goto first state (1)
    if ($c == 't' || $c == 'T')
        $dfa = 1;
 
    // On receiving 'H' or 'h'
    // goto second state (2)
    else if ($c == 'h' || $c == 'H')
        $dfa = 2;
 
    // else goto starting state (0)
    else
        $dfa = 0;
}
 
// This function is for
// the second state of DFA
function state2($c)
{
    global $dfa;
    // On receiving 'E' or 'e'
    // goto third state (3) else
    // goto starting state (0)
    if ($c == 'e' || $c == 'E')
        $dfa = 3;
    else
        $dfa = 0;
}
 
// This function is for
// the third state of DFA
function state3($c)
{
    global $dfa;
    // On receiving 'T' or 't'
    // goto first state (1) else
    // goto starting state (0)
    if ($c == 't' || $c == 'T')
        $dfa = 1;
    else
        $dfa = 0;
}
 
function isAccepted($str)
{
    global $dfa;
    // store length of string
    $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);
}
 
// Driver Code
$str = "forTHEgeeks";
if (isAccepted($str) == true)
    echo "ACCEPTED\n";
else
    echo "NOT ACCEPTED\n";
 
// This code is contributed by m_kit
?>


Javascript




<script>
 
    // Javascript program to
    // implement DFS that accepts
    // all string that do not end
    // with "THE"
     
    // dfa tells the number associated
    // with the present state
    let dfa = 0;
 
    // This function is for
    // the starting state (zeroth) of DFA
    function start(c)
    {
        // On receiving 'T' or 't' goto
        // first state (1)
        if (c == 't' || c == 'T')
            dfa = 1;
    }
 
    // This function is for the first state of DFA
    function state1(c)
    {
        // On receiving 'T' or 't' goto first state (1)
        if (c == 't' || c == 'T')
            dfa = 1;
 
        // On receiving 'H' or 'h' goto second state (2)
        else if (c == 'h' || c == 'H')
            dfa = 2;
 
        // else goto starting state (0)
        else
            dfa = 0;
    }
 
    // This function is for the second state of DFA
    function state2(c)
    {
        // On receiving 'E' or 'e' goto third state (3)
        // else goto starting state (0)
        if (c == 'e' || c == 'E')
            dfa = 3;
        else
            dfa = 0;
    }
 
    // This function is for the third state of DFA
    function state3(c)
    {
        // On receiving 'T' or 't' goto first state (1)
        // else goto starting state (0)
        if (c == 't' || c == 'T')
            dfa = 1;
        else
            dfa = 0;
    }
 
    function isAccepted(str)
    {
        // store length of string
        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>


Output : 

ACCEPTED

 

Time Complexity: O(n)
Auxiliary Space: O(1) since it is using constant space 



Previous Article
Next Article

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' &amp; '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 Explanation: The string "ababab" is starting with 'a' and
12 min read
Program to build a DFA that accepts Strings ending with abc
Given a string, str consists of characters 'a', 'b' &amp; '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--&gt;q0b: q0--&gt;q0a: q0--&gt;q1b: q1--&gt;q2c: q2--&gt;q3The string “cbabc”
11 min read
Program to build DFA that accepts the languages ending with "01" over the characters {0, 1}
Given a binary string str, the task is to build a DFA that accepts the languages ending with "01" over the characters {0, 1}. Examples: Input: str = "00011101" Output: String Accepted Explanation: As the given string ends with "01", therefore, it is accepted. Input: str = "00001111" Output: String Rejected Explanation: As the given string ends with
9 min read
DFA for strings not containing consecutive two a's and starting with 'a'
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 scenari
11 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 below DFA Machine will be q0-&gt;q1-&gt;q2-&gt;q2-
12 min read
DFA accepting all strings over w ∈(a,b)* which contains “aba” as a substring
Given a binary string S, the task is to write a program for DFA Machine that accepts a set of all strings over w ∈ (a, b)* which contains “aba” as a substring.Examples : Input-1 : ababa Output : Accepted Explanation : "ababa" consists "aba" Input-2 : abbbb Output : Not accepted Explanation : "abbbb" does not consist "aba" Approach : Below is the de
8 min read
DFA that Accepts All the Strings With At Least 1 'a' and Exactly 2 b's
Deterministic Finite Automata(DFA) is also known as Deterministic finite state automata(DSA). DFA is a Mathematical model of computation it is an abstract machine used to recognize patterns, it takes a string as input and changes its states accordingly when the desired terminal is found, then the transition occurs. At the time of transition, the au
3 min read
Build a DFA to accept Binary strings that starts or ends with "01"
Given binary string str, the task is to build a DFA that accepts the string if the string either starts with "01" or ends with "01". Input: str = "010000" Output: Accepted Explanation: The given string starts with "01". Input: str = "1100111" Output: Not Accepted Explanation: The given string neither starts with nor ends with "01". DFA or Determini
11 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'. Input: str = "abbaaaaa" Output: Accepted Explanati
10 min read
Program to construct a DFA to check if a given integer is unsigned or not
Given a string S that represents an integer, the task is to check if the given string S represents an unsigned integer or not by constructing the DFA. If the given string represents an unsigned integer, then print "Unsigned integer". Otherwise, print "Not an unsigned integer". Examples: Input: S = "+4554"Output: Not an unsigned integer Input: S = "
10 min read