Program to construct DFA accepting odd number of 0 s and odd number of 1 s | Function to check whether the given string is accepted by DFA or not ; 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 ; Given string ; Function Call
function checkValidDFA ( s ) { var final_state = 0 ; var previous_state = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( ( s [ i ] === " " && previous_state === 0 ) || ( s [ i ] === " " && previous_state === 3 ) ) { final_state = 1 ; } else if ( ( s [ i ] === " " && previous_state === 3 ) || ( s [ i ] === " " && previous_state === 0 ) ) { final_state = 2 ; } else if ( ( s [ i ] === " " && previous_state === 1 ) || ( s [ i ] === " " && previous_state === 2 ) ) { final_state = 0 ; } else if ( ( s [ i ] === " " && previous_state === 2 ) || ( s [ i ] === " " && previous_state === 1 ) ) { final_state = 3 ; } previous_state = final_state ; } if ( final_state === 3 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var s = " " ; checkValidDFA ( s ) ;