DFA for Strings not ending with " THE " | dfa tells the number associated with the present state ; This function is for the starting state ( zeroth ) of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; This function is for the first state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; On receiving ' H ' or ' h ' goto second state ( 2 ) ; else goto starting state ( 0 ) ; This function is for the second state of DFA ; On receiving ' E ' or ' e ' goto third state ( 3 ) else goto starting state ( 0 ) ; This function is for the third state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) else goto starting state ( 0 ) ; store length of string ; Driver code
let dfa = 0 ; function start ( c ) { if ( c == ' ' c == ' ' ) dfa = 1 ; } function state1 ( c ) { if ( c == ' ' c == ' ' ) dfa = 1 ; else if ( c == ' ' c == ' ' ) dfa = 2 ; else dfa = 0 ; } function state2 ( c ) { if ( c == ' ' c == ' ' ) dfa = 3 ; else dfa = 0 ; } function state3 ( c ) { if ( c == ' ' c == ' ' ) 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 = " " . split ( ' ' ) ; if ( isAccepted ( str ) == true ) document . write ( " " ) ; else document . write ( " " ) ;