NFA to accept strings that has atleast one character occurring in a multiple of 3 | NFA variable that keeps track of the state while transaction . ; This checks for invalid input . ; Function for the state Q2 ; State transitions ' a ' takes to Q4 , and ' b ' and ' c ' remain at Q2 ; Function for the state Q3 ; State transitions ' a ' takes to Q3 , and ' b ' and ' c ' remain at Q4 ; Function for the state Q4 ; State transitions ' a ' takes to Q2 , and ' b ' and ' c ' remain at Q3 ; Function for the state Q5 ; State transitions ' b ' takes to Q6 , and ' a ' and ' c ' remain at Q5 ; Function for the state Q6 ; State transitions ' b ' takes to Q7 , and ' a ' and ' c ' remain at Q7 ; Function for the state Q7 ; State transitions ' b ' takes to Q5 , and ' a ' and ' c ' remain at Q7 ; Function for the state Q8 ; State transitions ' c ' takes to Q9 , and ' a ' and ' b ' remain at Q8 ; Function for the state Q9 ; State transitions ' c ' takes to Q10 , and ' a ' and ' b ' remain at Q9 ; Function for the state Q10 ; State transitions ' c ' takes to Q8 , and ' a ' and ' b ' remain at Q10 ; Function to check for 3 a 's ; Function to check for 3 b 's ; Function to check for 3 c 's ; Driver Code ; If any of the states is true , that is , if either the number of a ' s ▁ or ▁ number ▁ of ▁ b ' s or number of c 's is a multiple of three, then the string is accepted
let nfa = 1 ; let flag = 0 ; function state1 ( c ) { if ( c == ' ' ) nfa = 2 ; else if ( c == ' ' c == ' ' ) nfa = 1 ; else flag = 1 ; } function state2 ( c ) { if ( c == ' ' ) nfa = 3 ; else if ( c == ' ' c == ' ' ) nfa = 2 ; else flag = 1 ; } function state3 ( c ) { if ( c == ' ' ) nfa = 1 ; else if ( c == ' ' c == ' ' ) nfa = 3 ; else flag = 1 ; } function state4 ( c ) { if ( c == ' ' ) nfa = 5 ; else if ( c == ' ' c == ' ' ) nfa = 4 ; else flag = 1 ; } function state5 ( c ) { if ( c == ' ' ) nfa = 6 ; else if ( c == ' ' c == ' ' ) nfa = 5 ; else flag = 1 ; } function state6 ( c ) { if ( c == ' ' ) nfa = 4 ; else if ( c == ' ' c == ' ' ) nfa = 6 ; else flag = 1 ; } function state7 ( c ) { if ( c == ' ' ) nfa = 8 ; else if ( c == ' ' c == ' ' ) nfa = 7 ; else flag = 1 ; } function state8 ( c ) { if ( c == ' ' ) nfa = 9 ; else if ( c == ' ' c == ' ' ) nfa = 8 ; else flag = 1 ; } function state9 ( c ) { if ( c == ' ' ) nfa = 7 ; else if ( c == ' ' c == ' ' ) nfa = 9 ; else flag = 1 ; } function checkA ( s , x ) { for ( let i = 0 ; i < x ; i ++ ) { if ( nfa == 1 ) state1 ( s [ i ] ) ; else if ( nfa == 2 ) state2 ( s [ i ] ) ; else if ( nfa == 3 ) state3 ( s [ i ] ) ; } if ( nfa == 1 ) { return true ; } else { nfa = 4 ; } } function checkB ( s , x ) { for ( let i = 0 ; i < x ; i ++ ) { if ( nfa == 4 ) state4 ( s [ i ] ) ; else if ( nfa == 5 ) state5 ( s [ i ] ) ; else if ( nfa == 6 ) state6 ( s [ i ] ) ; } if ( nfa == 4 ) { return true ; } else { nfa = 7 ; } } function checkC ( s , x ) { for ( let i = 0 ; i < x ; i ++ ) { if ( nfa == 7 ) state7 ( s [ i ] ) ; else if ( nfa == 8 ) state8 ( s [ i ] ) ; else if ( nfa == 9 ) state9 ( s [ i ] ) ; } if ( nfa == 7 ) { return true ; } } let s = " " ; let x = 5 ; if ( checkA ( s , x ) || checkB ( s , x ) || checkC ( s , x ) ) { document . write ( " " ) ; } else { if ( flag == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } }