Program to build a DFA that checks if a string ends with "01" or "10" | Java Program to DFA that accepts string ending with 01 or 10. ; End position is checked using the string length value . q0 is the starting state . q1 and q2 are intermediate states . q3 and q4 are final states . ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q1 , 1 takes to q2 ; Driver Code ; all state transitions are printed . if string is accpetable , YES is printed . else NO is printed
class GFG { static void q1 ( String s , int i ) { System . out . print ( " q1 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " NO " ) ; return ; } if ( s . charAt ( i ) == '0' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q2 ( String s , int i ) { System . out . print ( " q2 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " NO ▁ " ) ; return ; } if ( s . charAt ( i ) == '0' ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q3 ( String s , int i ) { System . out . print ( " q3 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " YES " ) ; return ; } if ( s . charAt ( i ) == '0' ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q4 ( String s , int i ) { System . out . print ( " q4 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " YES " ) ; return ; } if ( s . charAt ( i ) == '0' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q0 ( String s , int i ) { System . out . print ( " q0 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " NO " ) ; return ; } if ( s . charAt ( i ) == '0' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } public static void main ( String [ ] args ) { String s = "010101" ; System . out . print ( " State ▁ transitions ▁ are ▁ " ) ; q0 ( s , 0 ) ; } }