Open In App

Build a DFA to accept Binary strings that starts or ends with “01”

Given binary string str, the task is to build a DFA that accepts the string if the string either starts with “01” or ends with “01”. 

Input: str = “010000” 
Output: Accepted 
Explanation: 
The given string starts with “01”.



Input: str = “1100111” 
Output: Not Accepted 
Explanation: 
The given string neither starts with nor ends with “01”. 

DFA or Deterministic Finite Automata is a finite state machine that accepts a string(under some specific condition) if it reaches a final state, otherwise rejects it.
In DFA, there is no concept of memory, therefore we have to check the string character by character, beginning with the 0th character. The input set of characters for the problem is {0, 1}. For a DFA to be valid, there must a transition rule defined for each symbol of the input set at every state to a valid state. Therefore, the following steps are followed to design the DFA:



Step by Step Approach to design a DFA: 

Transition table and Transition rules of the above DFA:

State Input (0) Input (1)
—>A B D
B E C
C* C C
D E D
E E F
F* E D

Below is the implementation of the above approach:




// C++ program to check if a string
// either starts or ends with 01
#include<bits/stdc++.h>
using namespace std;
 
void stateA(string);
void stateB(string);
void stateC(string);
void stateD(string);
void stateE(string);
void stateF(string);
 
// Function for transition
// state A
void checkstateA(string n)
{
     
    // State transition to
    // B if the character is
    // 0
    if(n[0] == '0')
       stateB(n.substr(1));
        
    // State transition to
    // D if the character is
    // 1
    else
       stateD(n.substr(1));
}
 
// Function for transition
// state B        
void stateB(string n)
{
     
    // Check if the string has
    // ended
    if (n.length() == 0)
        cout << "string not accepted";
    else
    {
         
        // State transition to C
        // if the character is 1
        if(n[0] == '1')
            stateC(n.substr(1));
 
        // State transition to D
        // if the character is 0
        else
            stateD(n.substr(1));
    }    
}
 
// Function for transition
// state C
void stateC(string n)
{
    cout << "String accepted";
}
 
// Function for transition
// state D
void stateD(string n)
{
    if (n.length() == 0)
        cout << "string not accepted";
    else
    {
         
        // State transition to D
        // if the character is 1
        if (n[0] == '1')
            stateD(n.substr(1));
 
        // State transition to E
        // if the character is 0
        else
            stateE(n.substr(1));
    }
}
 
// Function for transition
// state E
void stateE(string n)
{
    if (n.length() == 0)
        cout << "string not accepted";
    else
    {
         
        // State transition to E
        // if the character is 0
        if(n[0] == '0')
            stateE(n.substr(1));
 
        // State transition to F
        // if the character is 1
        else
            stateF(n.substr(1));
    }
}
 
// Function for transition
// state F
void stateF(string n)
{
    if(n.length() == 0)
        cout << "string accepred";
    else
    {
         
        // State transition to D
        // if the character is 1
        if(n[0] == '1')
            stateD(n.substr(1));
 
        // State transition to E
        // if the character is 0
        else
            stateE(n.substr(1));
    }
}
 
// Driver code
int main()
{
    string n = "0100101";
     
    checkstateA(n);
    return 0;
}
 
// This code is contributed by chitranayal




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




# Python3 program to check if
# a string either starts or
# ends with 01
 
# Function for transition
# state A
def checkstateA(n):
 
    # State transition to
    # B if the character is
    # 0
    if(n[0]=='0'):
        stateB(n[1:])
 
    # State transition to
    # D if the character is
    # 1
    else:
        stateD(n[1:])
 
# Function for transition
# state B        
def stateB(n):
 
    # Check if the string has
    # ended
    if (len(n)== 0):
        print("string not accepted")
    else:   
     
        # State transition to C
        # if the character is 1
        if(n[0]=='1'):
            stateC(n[1:])
 
        # State transition to D
        # if the character is 0
        else:
            stateD(n[1:])
          
# Function for transition
# state C   
def stateC(n):
    print("String accepted")
  
# Function for transition
# state D
def stateD(n):
    if (len(n)== 0):
        print("string not accepted")
    else:   
 
        # State transition to D
        # if the character is 1
        if (n[0]=='1'):
            stateD(n[1:])
 
        # State transition to E
        # if the character is 0
        else:
            stateE(n[1:])
  
# Function for transition
# state E
def stateE(n):
    if (len(n)== 0):
        print("string not accepted")
    else:  
 
        # State transition to E
        # if the character is 0
        if(n[0]=='0'):
            stateE(n[1:])
 
        # State transition to F
        # if the character is 1
        else:
            stateF(n[1:])
  
# Function for transition
# state F
def stateF(n):
    if(len(n)== 0):
        print("string accepred")
    else:
 
        # State transition to D
        # if the character is 1
        if(n[0]=='1'):
            stateD(n[1:])
 
        # State transition to E
        # if the character is 0
        else:
            stateE(n[1:])
      
# Driver code
if __name__ == "__main__":
    n = "0100101"
    checkstateA(n)




// C# program to check if
// a string either starts
// or ends with 01
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
     
// Function for transition
// state A
static void checkstateA(string n)
{
  // State transition to
  // B if the character is
  // 0
  if(n[0] == '0')
    stateB(n.Substring(1));
 
  // State transition to
  // D if the character is
  // 1
  else
    stateD(n.Substring(1));
}
  
// Function for transition
// state B        
static void stateB(string n)
{    
  // Check if the string has
  // ended
  if (n.Length == 0)
  {
    Console.Write("string not accepted");
  }
  else
  {
    // State transition to C
    // if the character is 1
    if(n[0] == '1')
      stateC(n.Substring(1));
 
    // State transition to D
    // if the character is 0
    else
      stateD(n.Substring(1));
  }    
}
 
// Function for transition
// state C
static void stateC(string n)
{
  Console.Write("string accepted");
}
  
// Function for transition
// state D
static void stateD(string n)
{
  if (n.Length == 0)
    Console.Write("string not accepted");
  else
  {
 
    // State transition to D
    // if the character is 1
    if (n[0] == '1')
      stateD(n.Substring(1));
 
    // State transition to E
    // if the character is 0
    else
      stateE(n.Substring(1));
  }
}
  
// Function for transition
// state E
static void stateE(string n)
{
  if (n.Length == 0)
    Console.Write("string not accepted");
  else
  {
 
    // State transition to E
    // if the character is 0
    if(n[0] == '0')
      stateE(n.Substring(1));
 
    // State transition to F
    // if the character is 1
    else
      stateF(n.Substring(1));
  }
}
  
// Function for transition
// state F
static void stateF(string n)
{
  if(n.Length == 0)
    Console.Write("string accepted");
  else
  {
    // State transition to D
    // if the character is 1
    if(n[0] == '1')
      stateD(n.Substring(1));
 
    // State transition to E
    // if the character is 0
    else
      stateE(n.Substring(1));
  }
}
 
// Driver code
public static void Main(string []args)
{
  string n = "0100101";
  checkstateA(n);
}
}
 
// This code is contributed by rutvik_56




<script>
 
// JavaScript program to check if
// a string either starts
// or ends with 01
 
// Function for transition
// state A
function checkstateA(n)
{
  // State transition to
  // B if the character is
  // 0
  if(n[0] == '0')
    stateB(n.substr(1));
  
  // State transition to
  // D if the character is
  // 1
  else
    stateD(n.substr(1));
}
   
// Function for transition
// state B       
function stateB(n)
{   
  // Check if the string has
  // ended
  if (n.length == 0)
  {
    document.write("string not accepted");
  }
  else
  {
    // State transition to C
    // if the character is 1
    if(n[0] == '1')
      stateC(n.substr(1));
  
    // State transition to D
    // if the character is 0
    else
      stateD(n.substr(1));
  }   
}
  
// Function for transition
// state C
function stateC(n)
{
  document.write("string accepted");
}
   
// Function for transition
// state D
function stateD(n)
{
  if (n.length == 0)
    Console.Write("string not accepted");
  else
  {
  
    // State transition to D
    // if the character is 1
    if (n[0] == '1')
      stateD(n.substr(1));
  
    // State transition to E
    // if the character is 0
    else
      stateE(n.substr(1));
  }
}
   
// Function for transition
// state E
function stateE(n)
{
  if (n.length == 0)
    document.write("string not accepted");
  else
  {
  
    // State transition to E
    // if the character is 0
    if(n[0] == '0')
      stateE(n.substr(1));
  
    // State transition to F
    // if the character is 1
    else
      stateF(n.substr(1));
  }
}
   
// Function for transition
// state F
function stateF(n)
{
  if(n.length == 0)
    document.write("string accepted");
  else
  {
    // State transition to D
    // if the character is 1
    if(n[0] == '1')
      stateD(n.substr(1));
  
    // State transition to E
    // if the character is 0
    else
      stateE(n.substr(1));
  }
}
  
     
// Driver Code
 
    let n = "0100101";
  checkstateA(n);
           
</script>

Output
String accepted

Time Complexity: O(N)
Auxiliary Space: O(N)


Article Tags :