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 above mentioned forms of “the” are not accepted.
Determinitis 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
The time complexity of this program is O(n)
0 Comments