The Wayback Machine - https://web.archive.org/web/20240927190305/https://www.geeksforgeeks.org/dfa-string-least-two-0s-least-two-1s/
Open In App

DFA of a string with at least two 0’s and at least two 1’s

Last Updated : 31 May, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Problem – Draw deterministic finite automata (DFA) of a string with at least two 0’s and at least two 1’s. The first thing that come to mind after reading this question us that we count the number of 1’s and 0’s. Thereafter if they both are at least 2 the string is accepted else not accepted. But we do not have any concept of memory in a DFA so we cannot do it by this method.

Input  : 1 0 1 1 0 0
Output : Accepted

Input  : 1 1 1 0 1 
Output : Not accepted

Approach Used – The first thing we observe is that both 0’s and 1’s should be at least 2. If any of these is less than 2, then string will not be accepted. In this string will be accepted in only last case where both 0’s and 1’s will be at least 2.

State Count of 0 Count of 1
Q0 0 0
Q1 0 1
Q2 0 >=2
Q3 1 0
Q4 1 1
Q5 1 >=2
Q6 >=2 0
Q7 >=2 1
Q8 ACCEPTED >=2 >=2

Initially count of both 0 and 1 is zero and we are on state Q0.

  • Step-1: If input is 1 then count of 1 increases to 1. Goto state Q1 If input is 0 then count of 0 increases to 1. Goto state Q3
  • Step-2: If input is 1 then count of 1 increases to 2. Goto state Q2 If input is 0 then count of 0 increases to 1. Goto state Q4
  • Step-3: If input is 1 then count of 1 keeps increasing by 1. Remain in the same state If input is 0 then count of 0 increases to 1. Goto state Q5
  • Step-4: If input is 1 then count of 1 increases to 1. Goto state Q4 If input is 0 then count of 0 increases to 2. Goto state Q6
  • Step-5: If input is 1 then count of 1 increases to 2. Goto state Q5 If input is 0 then count of 0 increases to 2. Goto state Q7
  • Step-6: If input is 1 then count of 1 keeps increasing by 1. Remain in the same state. If input is 0 then count of 0 increases to 2. Goto state Q8
  • Step-7: If input is 1 then count of 1 increases to 1. Goto state Q7 If input is 0 then count of 0 keeps increasing by 1. Remain in the same state.
  • Step-8: If input is 1 then count of 1 increases to 2. Goto state Q8 If input is 0 then count of 0 keeps increasing by 1. Remain in the same state.
  • Step-9: If input is 1 then count of 1 keeps increasing by 1. Remain in the same state. If input is 0 then count of 0 keeps increasing by 1. Remain in the same state. If string is finished then ACCEPTED


Previous Article
Next Article

Similar Reads

Construct DFA with Σ = {0, 1} and Accept All String of Length at Least 2
Construct a DFA for a language accepting strings of length at least two, over input alphabets Σ = {0,1}. So that means in DFA, language consists of a string of length of at least 2 and can be greater than two. That means if DFA got the string of Length 0 or 1 then it will not accept it. A string of length zero means when the machine doesn't get any
5 min read
DFA for exactly one of a and at least one of b
Deterministic Finite Automata (DFA) is defined as an abstract mathematical concept which is used to solve various specific problems in different software and hardware.In this type of problems we have some given parameters according to which we should design DFA. In this article,two instructions are given: DFA should have one of a DFA should have at
2 min read
DFA that Accepts All the Strings With At Least 1 'a' and Exactly 2 b's
Deterministic Finite Automata(DFA) is also known as Deterministic finite state automata(DSA). DFA is a Mathematical model of computation it is an abstract machine used to recognize patterns, it takes a string as input and changes its states accordingly when the desired terminal is found, then the transition occurs. At the time of transition, the au
3 min read
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., [Tex]L={(01)^i (1)^{2j} \text{ where }i\geq1\text{ and }j\geq1} [/Tex] Examples: Input: str = "011111" Output: Accepted Explanation: The string follows the language as: (01)1(1)2*2 Input: str = "01111" Output: N
9 min read
Design a DFA that accepts a string containing 3 a's and 3 b's
Problem Statement: Design a Definite Finite Automata for accepting the permutation of Three a's and Three b's over the input {a, b} Input: S = "aaabbb" Output: Accepted Explanation: The input has three a and three b. Input: S = "abababa" Output: Accepted Explanation: The input has three a and three b. To design a DFA we need to check the input char
15+ min read
Construct DFA with Σ = {0, 1} and Accept All String of Length At Most 2
Construct a DFA for a language accepting strings of length at most two, over input alphabets Σ = {0,1}. So that means in DFA, language consisting of a string of lengths 0, 1, and 2 is present. Length of string zero means when the machine doesn't get any symbol as an input but it still accepts something(epsilon). Approach: First, try to make the lan
3 min read
DFA for strings not containing consecutive two a's and starting with 'a'
Prerequisite - Finite Automata Introduction Problem: Construct deterministic finite automata (DFA) for strings not containing consecutive two a's and starting with a. Explanation: Accept Strings that not contain consecutive two a's. Check if a given string contain consecutive two a's or not. The any occurrence of (b-z) should not affect the scenari
11 min read
DFA of a string in which 3rd symbol from RHS is ‘a’
Prerequisite – Finite Automata Introduction, DFA of a string in which 2nd symbol from RHS is ‘a’ Problem – Draw deterministic finite automata (DFA) of the language containing the set of all strings over {a, b} in which the 3rd symbol from RHS is ‘a’. The strings in which 3rd last symbol is “a” are: aaa, aab, aaab, aaaa, aabbaaa, bbbaba etc The DFA
2 min read
Design DFA in LEX Code that accepts the string having even binary number over input alphabet {0,1}
Problem Overview - Design a DFA in LEX Code that accepts the string having even binary number over input alphabet {0,1}. Example - Input : 1010 Output: Accepted Input : 1001 Output: Not Accepted Input: 23ab Output:Invalid Input:ab345 Output:Invalid Input:010101 Output:Not Accepted Approach :LEX provides us with an INITIAL state by default. So to ma
2 min read
DFA of a string in which 2nd symbol from RHS is 'a'
Draw deterministic finite automata (DFA) of the language containing the set of all strings over {a, b} in which 2nd symbol from RHS is 'a'. The strings in which 2nd last symbol is "a" are: aa, ab, aab, aaa, aabbaa, bbbab etc Input/Output INPUT : baba OUTPUT: NOT ACCEPTED INPUT: aaab OUTPUT: ACCEPTED Constructing the DFA of the given problem directl
10 min read
DFA in LEX code which accepts even number of zeros and even number of ones
Lex is a computer program that generates lexical analyzers, which is commonly used with the YACC parser generator. Lex, originally written by Mike Lesk and Eric Schmidt and described in 1975, is the standard lexical analyzer generator on many Unix systems, and an equivalent tool is specified as part of the POSIX standard. Lex reads an input stream
6 min read
DFA machines accepting odd number of 0’s or/and even number of 1’s
Prerequisite - Designing finite automata Problem - Construct a DFA machine over input alphabet [Tex]\sum_[/Tex]= {0, 1}, that accepts: Odd number of 0’s or even number of 1’s Odd number of 0’s and even number of 1’s Either odd number of 0’s or even number of 1’s but not the both together Solution - Let first design two separate machines for the two
3 min read
Program to build a DFA that accepts strings starting and ending with different character
Prerequisite: Deterministic Finite Automata Given a string, str consists of characters 'a' & 'b'. The task is to check whether string str starts and ends with different characters or not. If it does, print 'YES' with state transitions, else print 'NO'. Examples: Input: ababab Output: YES Explanation: The string "ababab" is starting with 'a' and
12 min read
Program to build a DFA to accept strings that start and end with same character
Given a string consisting of characters a and b, check if the string starts and ends with the same character or not. If it does, print 'Yes' else print 'No'.Examples: Input: str = "abbaaba" Output: Yes Explanation: The given input string starts and ends with same character 'a' So the states of the below DFA Machine will be q0->q1->q2->q2-
12 min read
Program to construct DFA accepting odd number of 0s and odd number of 1s
Given a binary string S, the task is to write a program for DFA Machine that accepts a string with odd numbers of 0s and 1s. Examples: Input: S = "010011"Output: AcceptedExplanation:The given string S contains odd number of zeros and ones. Input: S = "00000"Output: Not AcceptedExplanation:The given string S doesn't contains odd number of zeros and
7 min read
Construct a DFA which accept the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}
Problem: Construct a deterministic finite automata (DFA) for accepting the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}. Language L={w | Na(w) = Nb(w)mod 3} which means all string contain modulus of count of a's equal to modulus of count of b's by 3. Examples: Input: a a b b b Output: NOT ACCEPTED // n = 2, m=3 (2 mod 3! = 3 mod 3)
14 min read
DFA of alternate 0's and 1's
Regular Expression can be anything, from a terminal symbol, ∅, to union of two regular expressions (R 1 + R 2 ), their concatenation (R 1 R 2 ) or its closure R 1 * as well. Examples of Regular Expression : Regular expression of set of all strings of 0's and 1's starting with two zeros : 00(0+1)*Regular expression of set of all strings of 0's and 1
2 min read
Difference between DFA and NFA
In automata theory four types of finite automata are used to recognize the regular language among these two are DFA and NFA. Both have the same function but there are some differences in their structure and working. It is very essential for one to have a clear understanding of the difference between DFA and NFA for anyone to understand the computat
5 min read
DFA for accepting the language L = { anbm | n+m=even }
Design a deterministic finite automata(DFA) for accepting the language L =[Tex] {a^n b^{m} | m+n=even} [/Tex]For creating DFA for language, L = { a^n b^m ; n+m=even } use elementary mathematics, which says- even + even = even and odd + odd = even Examples: Input: a a b b // n = 2, m = 2, 2 + 2 = 4 (even) Output: ACCEPTED Input: a a a b b b b // n =
15 min read
DFA for Strings not ending with "THE"
Problem - Accept Strings that not ending with substring "THE". Check if a given string is ending with "the" or not. The different forms of "the" which are avoided in the end of the string are: "THE", "ThE", "THe", "tHE", "thE", "The", "tHe" and "the" All those strings that are ending with any of the above mentioned forms of "the" are not accepted.
12 min read
Union process in DFA
Prerequisite - Designing finite automata To perform the union operation on two deterministic finite automata (DFAs), the following steps can be taken: Create a new DFA with a new set of states, consisting of all the states from both original DFAs.Define the initial state of the new DFA to be the tuple (q1, q2), where q1 and q2 are the initial state
5 min read
Concatenation process in DFA
Prerequisite - Designing finite automata Designing a DFA for the set of string over {a, b} such that string of the language start with "a" and end with "b". There two desired language will be formed: L1 = {a, aab, aabab, .......} L2 = {b, bbab, bbabab, .......} In L1, starting element is "a" and in L2, ending element is b. State Transition Diagram
2 min read
Program to construct a DFA which accepts the language having all 'a' before all 'b'
Given a string S, the task is to design a Deterministic Finite Automata (DFA) for accepting the language L = {aNbM | N ≥ 0, M ≥ 0, N+M ≥ 1}. , i.e., a regular language L such that all 'a' occur before the first occurrence of 'b' {a, ab, aab, bb..., }. If the given string follows the given language L, then print “Accepted”. Otherwise, print “Not Acc
8 min read
Complementation process in DFA
Prerequisite – Design a Finite automata Suppose we have a DFA that is defined by ( Q, [Tex]\Sigma [/Tex], [Tex]\delta [/Tex], q0, F ) and it accepts the language L1. Then, the DFA which accepts the language L2 where L2 = ̅L1', will be defined as below: ( Q, [Tex]\Sigma[/Tex], [Tex]\delta[/Tex], q0, Q-F ) The complement of a DFA can be obtained by m
2 min read
DFA for accepting the language L = {an bm | n+m=odd}
Design a deterministic finite automata (DFA) for accepting the language [Tex]L = \{a^nb^m | \text{m+n=odd}\} [/Tex]For creating DFA for language L = {an bm | n+m=odd} use elementary mathematics which says, odd + even = odd, and even+odd = odd Examples: Input: a a b b b Output: ACCEPTED // n = 2, m = 3, 2 + 3 = 5 (odd) Input: a a a b b b Output: NOT
14 min read
Cross Product Operation in DFA
Prerequisite: Designing finite automata Let’s understand the cross product operation in Deterministic Finite Automata (DFA) with help of the below example- Designing a DFA for the set of string over {a, b} such that string of the language contains even number of a's and b's then desired language will be like below- L = {ε, aa, bb, abab, aab
2 min read
Program to construct a DFA which accept the language L = {anbm | n mod 2=0, m≥1}
Prerequisite - Finite Automata Introduction Problem: Design a deterministic finite automata (DFA) for accepting the language [Tex]L = \{a^nb^m | n\mod 2=0, m \geq 1\}[/Tex] Regular expression for above language L is, L = (aa)*.b+ Examples: Input: a a b b b Output: ACCEPTED // n = 2 (even) m=3 (>=1) Input: a a a b b b Output: NOT ACCEPTED // n =
10 min read
Construct DFA which interpreted as binary number is divisible by 2, 3, 4
Prerequisite: Finite Automata Introduction, Designing Finite Automata Problem-1: Construct DFA, which accepts set of all strings over {0, 1} which interpreted as a binary number is divisible by 2. Explanation: Consider the following inputs, {0, 01, 10, 11, 100, 101, 110........} The state transition diagram of the language will be like this: In thi
4 min read
Construct a DFA which accept the language L = {anbm | n > =1, (m) mod 3 = 1}
Problem: Construct a DFA which accept the language L = {anbm | n > =1, (m) mod 3 = 1}. Explanation: For constructing the DFA, the following things to be remember: [Tex](element)^*[/Tex] which means any no of elements, and [Tex](element)^+[/Tex] = [Tex](element).(element)^*[/Tex] which means any no of elements greater than 1. Examples: Input: a a
9 min read
DFA (Recognizer) for valid Pascal identifiers
Problem - Implement a recognizer for pascal identifiers based on a DFA that accepts strings belonging to the definition of the language of the same. Here is a regular definition for the set of Pascal identifiers that are defined as the set of strings of letters and digits beginning with a letter. letter : A | B | . . . | Z | a | b | . . . | z digit
6 min read