Build a DFA to accept Binary strings that starts or ends with "01" | Java program to check if a string either starts or ends with 01 ; Function for transition state A ; State transition to B if the character is 0 ; State transition to D if the character is 1 ; Function for transition state B ; Check if the string has ended ; State transition to C if the character is 1 ; State transition to D if the character is 0 ; Function for transition state C ; Function for transition state D ; State transition to D if the character is 1 ; State transition to E if the character is 0 ; Function for transition state E ; State transition to E if the character is 0 ; State transition to F if the character is 1 ; Function for transition state F ; State transition to D if the character is 1 ; State transition to E if the character is 0 ; Driver Code
import java . util . * ; class GFG { static void checkstateA ( String n ) { if ( n . charAt ( 0 ) == '0' ) stateB ( n . substring ( 1 ) ) ; else stateD ( n . substring ( 1 ) ) ; } static void stateB ( String n ) { if ( n . length ( ) == 0 ) System . out . println ( " string ▁ not ▁ accepted " ) ; else { if ( n . charAt ( 0 ) == '1' ) stateC ( n . substring ( 1 ) ) ; else stateD ( n . substring ( 1 ) ) ; } } static void stateC ( String n ) { System . out . println ( " String ▁ accepted " ) ; } static void stateD ( String n ) { if ( n . length ( ) == 0 ) System . out . println ( " string ▁ not ▁ accepted " ) ; else { if ( n . charAt ( 0 ) == '1' ) stateD ( n . substring ( 1 ) ) ; else stateE ( n . substring ( 1 ) ) ; } } static void stateE ( String n ) { if ( n . length ( ) == 0 ) System . out . println ( " string ▁ not ▁ accepted " ) ; else { if ( n . charAt ( 0 ) == '0' ) stateE ( n . substring ( 1 ) ) ; else stateF ( n . substring ( 1 ) ) ; } } static void stateF ( String n ) { if ( n . length ( ) == 0 ) System . out . println ( " string ▁ accepred " ) ; else { if ( n . charAt ( 0 ) == '1' ) stateD ( n . substring ( 1 ) ) ; else stateE ( n . substring ( 1 ) ) ; } } public static void main ( String args [ ] ) { String n = "0100101" ; checkstateA ( n ) ; } }