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

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:

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

**Step by Step Approach to design a DFA:**

**Step 1:**Minimum possible acceptable string is 0111, i.e, (01)^{1}(11)^{1}. So, create an initial state “A” that make transition of 0 to state “B” and then transition of 1 from “B” to state “C” then transition of 1 from “C” to “D”, then transition of 1 from “D” to “E” as shown in diagram make this stage “E” is final state.**Step 2:**Now, think about the string having consecutive (01) and then followed by consecutive (11) to end the string. Hence, when i>1, make a transition of “0” from state “C” to state “B” and make a transition of “1” from the state “E” to state “D”. Hence, strings like 010111, 011111, 0101111111, etc. are acceptable now.**Step 3:**We have done with all kind of strings possible to accept. But, there are few input alphabets which are not transited to any of the states. In this case, all these kind of input will be sent to some dead state to block their further transitions that are not acceptable. Input alphabets of the dead state will be sent to the dead state itself. Therefore, the final design of the DFA is:

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) ` ` ` ` ` |

*chevron_right*

*filter_none*

**Output:**

string accepted

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.

## Recommended Posts:

- Build a DFA to accept Binary strings that starts or ends with "01"
- Program to build a DFA to accept strings that start and end with same character
- Program to build a DFA that checks if a string ends with "01" or "10"
- Construct a DFA which accept the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}
- Design a DFA that accepts a string containing 3 a's and 3 b's
- Program to build a DFA that accepts strings starting and ending with different character
- Program to construct a DFA which accept the language L = {a
^{n}b^{m}| n mod 2=0, m≥1} - Construct a DFA which accept the language L = {a
^{n}b^{m}| n > =1, (m) mod 3 = 1} - Program to build DFA that accepts the languages ending with "01" over the characters {0, 1}
- DFA for strings not containing consecutive two a's and starting with 'a'
- DFA of a string with at least two 0’s and at least two 1’s
- Count of substrings of a binary string containing K ones
- Count of substrings of a Binary string containing only 1s
- Construct DFA which interpreted as binary number is divisible by 2, 3, 4
- String obtained by reversing and complementing a Binary string K times
- DFA of a string in which 2nd symbol from RHS is 'a'
- DFA of a string in which 3rd symbol from RHS is ‘a’
- Longest sub string of 0's in a binary string which is repeated K times
- DFA in LEX code which accepts even number of zeros and even number of ones
- DFA machines accepting odd number of 0’s or/and even number of 1’s

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.