# Build a DFA to accept a binary string containing “01” i times and “1” 2j times

Given a binary string str, the task is to build a DFA that accepts given binary string if it contains “01” i times and “1” 2j times, i.e., Examples:

Input: str = “011111”
Output: Accepted
Explanation:
The string follows the language as: (01)1(1)2*2

Input: str = “01111”
Output: Not Accepted

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

DFA or Deterministic Finite Automata is a finite state machine which 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:

1. Create initial stage and make the transition of 0 and 1 to next possible state.
2. Transition of 0 is always followed by transition of 1.
3. Make an initial state and transit its input alphabets, i.e, 0 and 1 to two different states.
4. Check for acceptance of string after each transition to ignore errors.
5. First, make DfA for minimum length string then go ahead step by step.
6. Define Final State(s) according to the acceptance of string.

Step by Step Approach to design a DFA:

Below is the implementation of the above approach:

 # Python program for the given  # language     # Function for the state A  def checkstatea(n):      if(len(n)%2!=0 or len(n)<4):          print("string not accepted")      else:              i=0            # State transition to B          # if the character is 0          if(n[i]=='0'):              stateb(n[1:])          else:              print("string not accepted")     # Function for the state B  def stateb(n):      i=0     if(n[i]=='0'):          print("string not accepted")         # State transition to C      # if the character is 1      else:          statec(n[1:])     # Function for the state C  def statec(n):      i=0        # State transition to D      # if the character is 1      if(n[i]=='1'):          stated(n[1:])         # State transition to B      # if the character is 0      else:          stateb(n[1:])     # Function for the state D  def stated(n):      i=0     if(len(n)==1):          if(n[i]=='1'):              print("string accepted")          else:              print("string not accepted")      else:             # State transition to E          # if the character is 1          if(n[i]=='1'):              statee(n[1:])          else:              print("string not accepted")        # Function for the state E       def statee(n):      i=0     if(len(n)==1):          if(n[i]=='0'):              print("string not accepted")          else:              print("string accepted")                  else:          if(n[i]=='0'):              print("string not accepted")          stated(n[1:])                  # Driver code  if __name__ == "__main__":         n = "011111"     checkstatea(n)         

Output:

string accepted


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.