Program to build a DFA to accept strings that start and end with same character | Function for the state Q1 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q2 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q3 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q4 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q0 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q3 ; Driver Code ; Since q0 is the starting state Send the string to q0
function q1 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } function q2 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } function q3 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } function q4 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } function q0 ( s , i ) { if ( i == s . length ) { document . write ( " " ) ; return ; } if ( s [ i ] == ' ' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } var s = " " ; q0 ( s , 0 ) ;