Construct DFA with Σ = {a, b} and Accept All String of Length at Least 2
Last Updated :
10 May, 2025
Construct a DFA for a language accepting strings of length at least two, over input alphabets Σ = {a, b}. This means that 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 symbol as an input.
Approach
- First, try to make the language with the help of string conditions.
- Find the minimum possible string that satisfies the condition of the length of at least two.
- So, the required strings with lengths of 2 and more will be the strings in the given language.
Examples
The accepted examples of the string which are of minimum 2 length are:
aa, ab, bb, aba, aab, bbab, ...
Strings which are not accepted are:
a, b and 𝜺
Concept
We need a language that will accept a string of length of at least 2 and can be greater than 2. So that means strings with lengths zero and one, will not be part of the language but apart from that, every string can be accepted by a given language. So firstly we will make DFA for the minimum string here of length 2 and then later, we don't have any restrictions of input alphabets a and b so we will accept everything with any sequence.
Let's discuss the stepwise construction of DFA:
Steps for Construction
Step 1 : Accepting minimum string of length 2
We need a DFA which will accept strings of length two. So for that, we'll make an initial state for DFA named q0 followed by two states q1 and q2 which will be able to accept a string of length of exactly 2.
Initializing the start state and conventions for statesStep 2 : Now see the DFA which will accept the string of length exact two:
Dfa for exactly 2 length stringsStep 2 : Accepting string of length at least two
Above DFA don't have any transition for a and b. So according to the given language, we need a string with a length of two and more than that, so we are done with a minimum string of length 2. For string that has a length greater than 2. We need a self-loop on state q2 for input alphabets a and b. So it can be capable of generating a string of length greater than two.
Dfa for minimum 2 length stringsThis is the final DFA which is capable of accepting strings of length at least two or lengths of two and more than that.
C++
#include <iostream>
#include <string>
class LengthAtLeastTwoDFA
{
private:
static const int Q0 = 0; // initial state
static const int Q1 = 1; // final state
public:
bool accept(std::string input)
{
int state = Q0;
for (char c : input)
{
switch (state)
{
case Q0:
if (c == 'a' || c == 'b')
{
state = Q1;
}
else
{
return false; // reject input
}
break;
case Q1:
if (c == 'a' || c == 'b')
{
state = Q1;
}
else
{
return true; // accept input
}
break;
}
}
return false; // reject input of length 1 or less
}
};
int main()
{
LengthAtLeastTwoDFA dfa;
std::string input;
std::cout << "Enter input string: ";
std::cin >> input;
if (dfa.accept(input))
{
std::cout << "Accepted\n";
}
else
{
std::cout << "Rejected\n";
}
return 0;
}
Java
// DFA that accepts all strings of length at least 2 over the alphabet {0, 1}
public class LengthAtLeastTwoDFA {
private static final int Q0 = 0; // initial state
private static final int Q1 = 1; // final state
public boolean accept(String input) {
int state = Q0;
for (char c : input.toCharArray()) {
switch (state) {
case Q0:
if (c == 'a' || c == 'b') {
state = Q1;
} else {
return false; // reject input
}
break;
case Q1:
if (c == 'a' || c == 'b') {
state = Q1;
} else {
return true; // accept input
}
break;
}
}
return false; // reject input of length 1 or less
}
}
Python
class LengthAtLeastTwoDFA:
Q0 = 0 # initial state
Q1 = 1 # final state
def accept(self, input_str):
state = LengthAtLeastTwoDFA.Q0
for c in input_str:
if state == LengthAtLeastTwoDFA.Q0:
if c == 'a' or c == 'b':
state = LengthAtLeastTwoDFA.Q1
else:
return False # reject input
elif state == LengthAtLeastTwoDFA.Q1:
if c == 'a' or c == 'b':
state = LengthAtLeastTwoDFA.Q1
else:
return True # accept input
return False # reject input of length 1 or less
if __name__ == '__main__':
dfa = LengthAtLeastTwoDFA()
input_str = input("Enter input string: ")
if dfa.accept(input_str):
print("Accepted")
else:
print("Rejected")
C#
using System;
public class LengthAtLeastTwoDFA
{
private const int Q0 = 0; // initial state
private const int Q1 = 1; // final state
public bool Accept(string input)
{
int state = Q0;
foreach (char c in input)
{
switch (state)
{
case Q0:
if (c == 'a' || c == 'b')
{
state = Q1;
}
else
{
return false; // reject input
}
break;
case Q1:
if (c == 'a' || c == 'b')
{
state = Q1;
}
else
{
return true; // accept input
}
break;
}
}
return false; // reject input of length 1 or less
}
}
JavaScript
class LengthAtLeastTwoDFA {
static Q0 = 0; // initial state
static Q1 = 1; // final state
accept(inputStr) {
let state = LengthAtLeastTwoDFA.Q0;
for (const c of inputStr) {
if (state === LengthAtLeastTwoDFA.Q0) {
if (c === 'a' || c === 'b') {
state = LengthAtLeastTwoDFA.Q1;
} else {
return false; // reject input
}
} else if (state === LengthAtLeastTwoDFA.Q1) {
if (c === 'a' || c === 'b') {
state = LengthAtLeastTwoDFA.Q1;
} else {
return true; // accept input
}
}
}
return false; // reject input of length 1 or less
}
}
const dfa = new LengthAtLeastTwoDFA();
const inputStr = prompt("Enter input string: ");
if (dfa.accept(inputStr)) {
console.log("Accepted");
} else {
console.log("Rejected");
}
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 Σ = {a, b}. This means that 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
5 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 someth
3 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 tr
3 min read
DFA of a string with at least two 0âs and at least two 1âs
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
3 min read
Construct a DFA that Start With aa or bb
Prerequisite: Designing Finite Automata DFA (Deterministic Finite Automata or Acceptor) is a finite state machine that accepts or rejects strings of symbols. DFA accepts the string if it reaches the final state otherwise it rejects it. In these types of problems, we have some given parameters accord
2 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
15+ min read
LEX Code which accepts string containing third last element âaâ over input alphabet {a, b}
In this article, we will discuss the DFA in LEX Code which accepts a string containing the third-last element âaâ over input alphabet {a, b} with the help of example. Let's discuss it one y one. Pre-requisite - Designing Finite Automata Problem Overview : Design a DFA in LEX Code which accepts a str
2 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 b
12 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
14 min read
LEX Code that accepts the string ending with 'abb' over input alphabet {a,b}
In this article, we will discuss the LEX Code that accepts the string ending with 'abb' over input alphabet {a,b} and will see the implementation using LEX code and will understand the approach. Let's discuss it one by one. Problem Overview :LEX Code that accepts the string ending with 'abb' over in
2 min read