Program to construct DFA accepting odd number of 0 s and odd number of 1 s | Java program for the above approach ; Function to check whether the given string is accepted by DFA or not ; Stores initial state of DFA ; Stores final state of DFA ; Stores previous state of DFA ; Iterate through the string ; Checking for all combinations ; Update the previous_state ; If final state is reached ; Otherwise ; Driver Code ; Given string ; Function Call
import java . util . * ; class GFG { static void checkValidDFA ( String s ) { int initial_state = 0 ; int final_state = 0 ; int previous_state = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( s . charAt ( i ) == '0' && previous_state == 0 ) || ( s . charAt ( i ) == '1' && previous_state == 3 ) ) { final_state = 1 ; } else if ( ( s . charAt ( i ) == '0' && previous_state == 3 ) || ( s . charAt ( i ) == '1' && previous_state == 0 ) ) { final_state = 2 ; } else if ( ( s . charAt ( i ) == '0' && previous_state == 1 ) || ( s . charAt ( i ) == '1' && previous_state == 2 ) ) { final_state = 0 ; } else if ( ( s . charAt ( i ) == '0' && previous_state == 2 ) || ( s . charAt ( i ) == '1' && previous_state == 1 ) ) { final_state = 3 ; } previous_state = final_state ; } if ( final_state == 3 ) { System . out . println ( " Accepted " ) ; } else { System . out . println ( " Not ▁ Accepted " ) ; } } public static void main ( String args [ ] ) { String s = "010011" ; checkValidDFA ( s ) ; } }