DFA accepting all strings over w ∈(a,b)* which contains “aba” as a substring
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 : "abbbb" 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 as follows:
C
#include <stdio.h>
#include <string.h>
void checkValidDFA( char s[] )
{
int initial_state = 0;
int previous_state = initial_state;
int final_state;
for ( int i = 0; i < strlen (s); i++)
{
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;
}
previous_state = final_state;
}
if (final_state == 3)
{
printf ( "Accepted" );
}
else
{
printf ( "Not Accepted" );
}
}
int main()
{
char s[] = "ababa" ;
checkValidDFA(s);
}
|
C++
#include <cstring>
#include <iostream>
using namespace std;
void checkValidDFA(string s)
{
int initial_state = 0;
int previous_state = initial_state;
int final_state;
for ( int i = 0; i < s.length(); i++) {
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;
}
previous_state = final_state;
}
if (final_state == 3) {
cout << "Accepted" << endl;
}
else {
cout << "Not Accepted" << endl;
}
}
int main()
{
string s = "ababa" ;
checkValidDFA(s);
}
|
Java
import java.util.*;
public class Main {
public static void checkValidDFA(String s) {
int initial_state = 0 ;
int previous_state = initial_state;
int final_state = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if ((previous_state == 0 && s.charAt(i) == 'a' ) ||
(previous_state == 1 && s.charAt(i) == 'a' )) {
final_state = 1 ;
}
if ((previous_state == 0 && s.charAt(i) == 'b' ) ||
(previous_state == 2 && s.charAt(i) == 'b' )) {
final_state = 0 ;
}
if (previous_state == 1 && s.charAt(i) == 'b' ) {
final_state = 2 ;
}
if ((previous_state == 2 && s.charAt(i) == 'a' ) ||
(previous_state == 3 )) {
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 = "ababa" ;
checkValidDFA(s);
}
}
|
Python
def checkValidDFA(s):
initial_state = 0
previous_state = initial_state
final_state = None
for i in range ( len (s)):
if (previous_state = = 0 and s[i] = = 'a' ) or (previous_state = = 1 and s[i] = = 'a' ):
final_state = 1
if (previous_state = = 0 and s[i] = = 'b' ) or (previous_state = = 2 and s[i] = = 'b' ):
final_state = 0
if previous_state = = 1 and s[i] = = 'b' :
final_state = 2
if (previous_state = = 2 and s[i] = = 'a' ) or (previous_state = = 3 ):
final_state = 3
previous_state = final_state
if final_state = = 3 :
print ( "Accepted" )
else :
print ( "Not Accepted" )
if __name__ = = '__main__' :
s = "ababa"
checkValidDFA(s)
|
C#
using System;
class Program
{
static void checkValidDFA( string s)
{
int initial_state = 0;
int previous_state = initial_state;
int final_state = 0;
for ( int i = 0; i < s.Length; i++)
{
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;
}
previous_state = final_state;
}
if (final_state == 3)
{
Console.WriteLine( "Accepted" );
}
else
{
Console.WriteLine( "Not Accepted" );
}
}
static void Main()
{
string s = "ababa" ;
checkValidDFA(s);
}
}
|
Javascript
function checkValidDFA(s) {
let initial_state = 0;
let previous_state = initial_state;
let final_state = null ;
for (let i = 0; i < s.length; i++) {
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;
}
previous_state = final_state;
}
if (final_state === 3) {
console.log( "Accepted" );
}
else {
console.log( "Not Accepted" );
}
}
let s = "ababa" ;
checkValidDFA(s);
|
Time Complexity : O(N)
Auxiliary Space : O(1)
Last Updated :
05 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...