Given a binary string S, the task is to write a program for DFA Machine that accepts a set of all strings over w ∈ (a, b)* which contains “aba” as a substring.
Examples :
Input-1 : ababa Output : Accepted Explanation : "ababa" consists "aba" Input-2 : abbbb Output : Not accepted Explanation : "ababa" does not consist "aba"
Approach : Below is the designed DFA Machine for the given problem. Construct a transition table for DFA states and analyze the transitions between each state. Below are the steps –
Desired Language :
L = {aba, baba, abab, aababbb.....}
Explanation :
- Firstly, there will be 4states.(say q0, q1, q2, q3) , with q0 being initial state and q3 being final state.
- Initially we will be in q0 state, now we start reading the given string.
- When we read ‘b’, we will remain in the same state
- If we read ‘a’, then it transits to state q1.
3. Assuming that now we are in q1 state.
- When we read ‘a’, we will remain in the same state.
- If we read ‘b’, we will transits to state q2.
4. Assuming that now we are in q2 state.
- If we read ‘a’, it transits to state q3.
- If we read ‘b’, it transits to state q0.
5. Assuming we are in final state(q3)
- We remain in the same state, when we read ‘a’ or ‘b’.
6. All strings which are accepted by this DFA will have “aba” as its substring.
Transition Table :
Current state | Final state | |
---|---|---|
a | b | |
q0 | q1 | q0 |
q1 | q1 | q2 |
q2 | q3 | q0 |
q3 | q3 | q3 |
Below is the implementation of the above approach –
C++
// C++ program for the above approach #include <cstring> #include <iostream> using namespace std; // Function to check whether the given // string is accepted by DFA or not void checkValidDFA(string s) { // Stores initial state of DFA int initial_state = 0; // Stores previous state of DFA int previous_state = initial_state; // Stores final state of DFA int final_state; // Iterate through the string for ( int i = 0; i < s.length(); i++) { // Checking for all combinations if ((previous_state == 0 && s[i] == 'a' ) || (previous_state == 1 && s[i] == 'a' )) { final_state = 1; } if ((previous_state == 0 && s[i] == 'b' ) || (previous_state == 2 && s[i] == 'b' )) { final_state = 0; } if (previous_state == 1 && s[i] == 'b' ) { final_state = 2; } if ((previous_state == 2 && s[i] == 'a' ) || (previous_state == 3)) { final_state = 3; } // Update the previous_state previous_state = final_state; } // If final state is reached if (final_state == 3) { cout << "Accepted" << endl; } // Otherwise else { cout << "Not Accepted" << endl; } } // Driver Code int main() { // Given string string s = "ababa" ; // Function Call checkValidDFA(s); } |
C
// C++ program for the above approach #include <stdio.h> #include <string.h> // Function to check whether the given // string is accepted by DFA or not void checkValidDFA( char s[] ) { // Stores initial state of DFA int initial_state = 0; // Stores previous state of DFA int previous_state = initial_state; // Stores final state of DFA int final_state; // Iterate through the string for ( int i = 0; i < strlen (s); i++) { // Checking for all combinations if ((previous_state == 0 && s[i] == 'a' ) || (previous_state == 1 && s[i] == 'a' )) { final_state = 1; } if ((previous_state == 0 && s[i] == 'b' ) || (previous_state == 2 && s[i] == 'b' )) { final_state = 0; } if (previous_state == 1 && s[i] == 'b' ) { final_state = 2; } if ((previous_state == 2 && s[i] == 'a' ) || (previous_state == 3)) { final_state = 3; } // Update the previous_state previous_state = final_state; } // If final state is reached if (final_state == 3) { printf ( "Accepted" ); } // Otherwise else { printf ( "Not Accepted" ); } } // Driver Code int main() { // Given string char s[] = "ababa" ; // Function Call checkValidDFA(s); } |
Output :
Accepted
Time Complexity : O(N)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.