Program to build a DFA to accept strings that start and end with same character | Java Program for DFA that accepts string if it starts and ends 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
class GFG { static void q1 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " Yes " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q2 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " No " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q3 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " Yes " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q4 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " No " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q0 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " No " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } public static void main ( String [ ] args ) { String s = " abbaabb " ; q0 ( s , 0 ) ; } }